Class: ActiveMatrix::Bot::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/active_matrix/bot/command_parser.rb

Overview

Parses command arguments with support for flags and quoted strings

Examples:

Basic parsing

parser = CommandParser.new('search "hello world" --verbose')
parser.args        # => ["search", "hello world"]
parser.flags       # => { "verbose" => true }

With key-value flags

parser = CommandParser.new('greet --name=Alice --formal')
parser.positional_args  # => []
parser.flags            # => { "name" => "Alice", "formal" => true }

Constant Summary collapse

COMMAND_PREFIXES =

Command prefixes recognized by the parser

['/', '!'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, prefixes: COMMAND_PREFIXES) ⇒ CommandParser

Returns a new instance of CommandParser.

Parameters:

  • input (String)

    Raw command input

  • prefixes (Array<String>) (defaults to: COMMAND_PREFIXES)

    Command prefixes to recognize



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

#argsObject (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_nameObject (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_argsObject (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_inputObject (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)

Returns:

  • (Boolean)


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

Parameters:

  • name (String)

    Flag name

  • default (Object) (defaults to: nil)

    Default value if flag not set

Returns:

  • (Object)


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

Parameters:

  • name (String)

    Flag name (without dashes)

Returns:

  • (Boolean)


95
96
97
# File 'lib/active_matrix/bot/command_parser.rb', line 95

def flag?(name)
  flags.key?(name.to_s)
end

#flagsHash<String, Object>

Get only flag arguments

Returns:

  • (Hash<String, Object>)


87
88
89
# File 'lib/active_matrix/bot/command_parser.rb', line 87

def flags
  parse_flags[:flags]
end

#formatted_commandString

Formatted command string

Returns:

  • (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_flagsHash

Parse flags and positional arguments

Returns:

  • (Hash)

    Hash with :flags and :args keys



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_argsArray<String>

Get only positional arguments (no flags)

Returns:

  • (Array<String>)


80
81
82
# File 'lib/active_matrix/bot/command_parser.rb', line 80

def positional_args
  parse_flags[:args]
end

#prefixString?

Get the prefix used in the command

Returns:

  • (String, nil)


45
46
47
# File 'lib/active_matrix/bot/command_parser.rb', line 45

def prefix
  @prefixes.find { |p| @raw_input.start_with?(p) }
end