@@ -22,12 +22,15 @@ def chat(message, user, room_id = "123")
2222 metadata [ "regex" ] = Regexp . new ( "^#{ metadata [ "regex" ] } $" , "i" )
2323 metadata
2424 }
25- matcher = matchers . find { |matcher | matcher [ "regex" ] . match ( message ) }
2625
27- raise NoMatchingCommandRegex . new ( "No command matches ' #{ message } '" ) unless matcher
26+ named_params , command = extract_named_params ( message )
2827
29- match_data = matcher [ "regex" ] . match ( message )
30- jsonrpc_params = { }
28+ matcher = matchers . find { |matcher | matcher [ "regex" ] . match ( command ) }
29+
30+ raise NoMatchingCommandRegex . new ( "No command matches '#{ command } '" ) unless matcher
31+
32+ match_data = matcher [ "regex" ] . match ( command )
33+ jsonrpc_params = named_params . dup
3134 matcher [ "params" ] . each do |param |
3235 jsonrpc_params [ param ] = match_data [ param . to_sym ]
3336 end
@@ -47,4 +50,18 @@ def chatop_error
4750 json_response = JSON . load ( response . body )
4851 json_response [ "error" ] [ "message" ]
4952 end
53+
54+ def extract_named_params ( command_string )
55+ params = { }
56+
57+ while last_index = command_string . rindex ( " --" )
58+ arg = command_string [ last_index ..-1 ]
59+ matches = arg . match ( / --(\S +)(.*)/ )
60+ params [ matches [ 1 ] ] = matches [ 2 ] . strip
61+ command_string = command_string . slice ( 0 , last_index )
62+ end
63+
64+ command_string = command_string . strip
65+ [ params , command_string ]
66+ end
5067end
0 commit comments