Class: ArgParser::CommandArgument
- 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
-
#commands ⇒ Array<Symbol] List of valid commands that may be specified.
Array<Symbol] List of valid commands that may be specified.
Attributes inherited from Argument
#default, #description, #key, #on_parse, #required, #short_key, #usage_break
Instance Method Summary collapse
-
#<<(cmd_instance) ⇒ Object
Adds a specific command verb/value to this command argument.
-
#[](cmd_val) ⇒ CommandInstance
Return the CommandInstance for a particular command value.
-
#initialize(key, desc, opts = {}) ⇒ CommandArgument
constructor
Create an instance of a CommandArgument, a positional argument that indicates a particular command to be invoked.
- #to_s ⇒ Object
- #to_use ⇒ Object
Methods inherited from Argument
Constructor Details
#initialize(key, desc, opts = {}) ⇒ CommandArgument
Create an instance of a CommandArgument, a positional argument that indicates a particular command to be invoked.
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
#commands ⇒ Array<Symbol] List of valid commands that may be specified.
Returns 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
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
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_s ⇒ Object
188 189 190 |
# File 'lib/arg-parser/argument.rb', line 188 def to_s @usage_value end |
#to_use ⇒ Object
192 193 194 |
# File 'lib/arg-parser/argument.rb', line 192 def to_use required? ? usage_value : "[#{usage_value}]" end |