Class: ArgParser::CommandArgument

Inherits:
Argument
  • Object
show all
Defined in:
lib/arg-parser/argument.rb

Overview

An argument type that defines a command. Each command placeholder can have one or more command arguments (i.e. allowed values). Depending on the command, different additional arguments may then be specified in the command’s ArgumentSet.

Instance Attribute Summary collapse

Attributes inherited from Argument

#default, #description, #key, #on_parse, #required, #short_key, #usage_break

Instance Method Summary collapse

Methods inherited from Argument

lookup, register, to_key

Constructor Details

#initialize(key, desc, opts = {}) ⇒ CommandArgument

Create an instance of a CommandArgument, a positional argument that indicates a particular command to be invoked.

Parameters:

  • key (String|Symbol)

    The value under which the specified command can be retrieved following parsing (e.g. :command)

  • desc (String)

    A description for the command argument.

  • opts (Hash) (defaults to: {})

    An options hash. See Argument#initialize for supported option values.



166
167
168
169
170
171
# File 'lib/arg-parser/argument.rb', line 166

def initialize(key, desc, opts = {})
    super(key, desc, opts)
    @commands = {}
    @required = opts.fetch(:required, !opts.has_key?(:default))
    @usage_value = opts.fetch(:usage_value, key.to_s.gsub('_', '-').upcase)
end

Instance Attribute Details

#commandsArray<Symbol] List of valid commands that may be specified.

Returns Array<Symbol] List of valid commands that may be specified.

Returns:

  • (Array<Symbol] List of valid commands that may be specified.)

    Array<Symbol] List of valid commands that may be specified.



155
156
157
# File 'lib/arg-parser/argument.rb', line 155

def commands
  @commands
end

Instance Method Details

#<<(cmd_instance) ⇒ Object

Adds a specific command verb/value to this command argument

Raises:

  • (ArgumentError)


174
175
176
177
# File 'lib/arg-parser/argument.rb', line 174

def <<(cmd_instance)
    raise ArgumentError, "must be a CommandInstance object" unless cmd_instance.is_a?(CommandInstance)
    @commands[cmd_instance.command_value] = cmd_instance
end

#[](cmd_val) ⇒ CommandInstance

Return the CommandInstance for a particular command value

Parameters:

  • cmd_val (String|Symbol)

    A command token identifying the command

Returns:



183
184
185
186
# File 'lib/arg-parser/argument.rb', line 183

def [](cmd_val)
    k = Argument.to_key(cmd_val)
    @commands[k]
end

#to_sObject



188
189
190
# File 'lib/arg-parser/argument.rb', line 188

def to_s
    @usage_value
end

#to_useObject



192
193
194
# File 'lib/arg-parser/argument.rb', line 192

def to_use
    required? ? usage_value : "[#{usage_value}]"
end