Class: ActiveMatrix::Bot::CommandParser
- Inherits:
-
Object
- Object
- ActiveMatrix::Bot::CommandParser
- Defined in:
- lib/active_matrix/bot/command_parser.rb
Overview
Parses command arguments with support for flags and quoted strings
Constant Summary collapse
- COMMAND_PREFIXES =
Command prefixes recognized by the parser
['/', '!'].freeze
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#command_name ⇒ Object
readonly
Returns the value of attribute command_name.
-
#raw_args ⇒ Object
readonly
Returns the value of attribute raw_args.
-
#raw_input ⇒ Object
readonly
Returns the value of attribute raw_input.
Instance Method Summary collapse
-
#command? ⇒ Boolean
Check if input is a valid command (has prefix and command name).
-
#flag(name, default = nil) ⇒ Object
Get a flag value.
-
#flag?(name) ⇒ Boolean
Check if a specific flag is set.
-
#flags ⇒ Hash<String, Object>
Get only flag arguments.
-
#formatted_command ⇒ String
Formatted command string.
-
#initialize(input, prefixes: COMMAND_PREFIXES) ⇒ CommandParser
constructor
A new instance of CommandParser.
-
#parse_flags ⇒ Hash
Parse flags and positional arguments.
-
#positional_args ⇒ Array<String>
Get only positional arguments (no flags).
-
#prefix ⇒ String?
Get the prefix used in the command.
Constructor Details
#initialize(input, prefixes: COMMAND_PREFIXES) ⇒ CommandParser
Returns a new instance of CommandParser.
25 26 27 28 29 30 31 32 33 |
# File 'lib/active_matrix/bot/command_parser.rb', line 25 def initialize(input, prefixes: COMMAND_PREFIXES) @raw_input = input.to_s.strip @prefixes = prefixes @command_name = nil @args = [] @raw_args = '' @parsed_flags = nil parse! end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
18 19 20 |
# File 'lib/active_matrix/bot/command_parser.rb', line 18 def args @args end |
#command_name ⇒ Object (readonly)
Returns the value of attribute command_name.
18 19 20 |
# File 'lib/active_matrix/bot/command_parser.rb', line 18 def command_name @command_name end |
#raw_args ⇒ Object (readonly)
Returns the value of attribute raw_args.
18 19 20 |
# File 'lib/active_matrix/bot/command_parser.rb', line 18 def raw_args @raw_args end |
#raw_input ⇒ Object (readonly)
Returns the value of attribute raw_input.
18 19 20 |
# File 'lib/active_matrix/bot/command_parser.rb', line 18 def raw_input @raw_input end |
Instance Method Details
#command? ⇒ Boolean
Check if input is a valid command (has prefix and command name)
38 39 40 |
# File 'lib/active_matrix/bot/command_parser.rb', line 38 def command? !@command_name.nil? && @prefixes.any? { |prefix| @raw_input.start_with?(prefix) } end |
#flag(name, default = nil) ⇒ Object
Get a flag value
104 105 106 |
# File 'lib/active_matrix/bot/command_parser.rb', line 104 def flag(name, default = nil) flags.fetch(name.to_s, default) end |
#flag?(name) ⇒ Boolean
Check if a specific flag is set
95 96 97 |
# File 'lib/active_matrix/bot/command_parser.rb', line 95 def flag?(name) flags.key?(name.to_s) end |
#flags ⇒ Hash<String, Object>
Get only flag arguments
87 88 89 |
# File 'lib/active_matrix/bot/command_parser.rb', line 87 def flags parse_flags[:flags] end |
#formatted_command ⇒ String
Formatted command string
111 112 113 114 115 |
# File 'lib/active_matrix/bot/command_parser.rb', line 111 def formatted_command return '' unless command? [@command_name, *@args].join(' ') end |
#parse_flags ⇒ Hash
Parse flags and positional arguments
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_matrix/bot/command_parser.rb', line 52 def parse_flags @parse_flags ||= begin flags = {} positional = [] @args.each do |arg| case arg when /\A--([^=]+)=(.+)\z/ # --key=value format flags[Regexp.last_match(1)] = Regexp.last_match(2) when /\A--(.+)\z/ # --flag format (boolean) flags[Regexp.last_match(1)] = true when /\A-([a-zA-Z]+)\z/ # Short flags like -v, -abc (multiple) Regexp.last_match(1).chars.each { |c| flags[c] = true } else positional << arg end end { flags: flags, args: positional } end end |
#positional_args ⇒ Array<String>
Get only positional arguments (no flags)
80 81 82 |
# File 'lib/active_matrix/bot/command_parser.rb', line 80 def positional_args parse_flags[:args] end |
#prefix ⇒ String?
Get the prefix used in the command
45 46 47 |
# File 'lib/active_matrix/bot/command_parser.rb', line 45 def prefix @prefixes.find { |p| @raw_input.start_with?(p) } end |