Class: ParseArgv::Result
- Inherits:
-
Object
- Object
- ParseArgv::Result
- Defined in:
- lib/parse-argv.rb
Overview
The result of a complete parsing process made with from. It contains all arguments parsed from the command line and the defined commands.
Defined Under Namespace
Instance Attribute Summary collapse
-
#all_commands ⇒ Array<Command>
readonly
All defined commands.
-
#current_command ⇒ Command
readonly
Command used for this result.
-
#main_command ⇒ Command
readonly
Main command if subcommands are used.
Instance Method Summary collapse
-
#[](name) ⇒ Value?
Get an argument as Value which can be converted.
-
#error!(message, code = 1) ⇒ Object
Calls the error handler defined by on_error.
-
#fetch(name, *args, &block) ⇒ Object
Try to fetch the value for the given argument +name+.
-
#find_command(name) ⇒ Command?
Find the command with given +name+.
- #to_h(&block) ⇒ Object
-
#to_s ⇒ String
Returns the help text of the #current_command.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object (private)
All command line attributes are read-only attributes for this instance.
425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/parse-argv.rb', line 425 def method_missing(sym, *args) args.empty? or raise( ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" ) return @rgs.key?(sym) ? @rgs[sym] : super unless sym.end_with?('?') sym = sym[..-2].to_sym @rgs.key?(sym) or return super value = @rgs[sym] value != nil && value != false end |
Instance Attribute Details
#all_commands ⇒ Array<Command> (readonly)
Returns all defined commands.
268 269 270 |
# File 'lib/parse-argv.rb', line 268 def all_commands @all_commands end |
#current_command ⇒ Command (readonly)
Returns command used for this result.
273 274 275 |
# File 'lib/parse-argv.rb', line 273 def current_command @current_command end |
#main_command ⇒ Command (readonly)
Returns main command if subcommands are used.
278 279 280 |
# File 'lib/parse-argv.rb', line 278 def main_command @main_command end |
Instance Method Details
#[](name) ⇒ Value?
Get an argument as Value which can be converted.
298 299 300 301 |
# File 'lib/parse-argv.rb', line 298 def [](name) name = name.to_sym @rgs.key?(name) ? Value.new(@rgs[name], argument_error(name)) : nil end |
#error!(message, code = 1) ⇒ Object
Calls the error handler defined by ParseArgv.on_error.
By default the error handler writes the Error#message prefixed with related Error#command name to $std_err and terminates the application with Error#code.
If no error handler was defined an Error will be raised.
This method is useful whenever your application needs signal an critical error case (and should be terminated).
321 322 323 324 |
# File 'lib/parse-argv.rb', line 321 def error!(, code = 1) error = Error.new(current_command, , code) ParseArgv.on_error&.call(error) || raise(error) end |
#fetch(name) ⇒ Object #fetch(name, default_value) ⇒ Object #fetch(name) {|name| ... } ⇒ Object
Try to fetch the value for the given argument +name+.
353 354 355 356 357 358 |
# File 'lib/parse-argv.rb', line 353 def fetch(name, *args, &block) name = name.to_sym value = @args[name] return value unless value.nil? args.empty? ? (block || ATTRIBUTE_ERROR).call(name) : args[0] end |
#find_command(name) ⇒ Command?
Find the command with given +name+.
367 368 369 370 371 |
# File 'lib/parse-argv.rb', line 367 def find_command(name) return if name.nil? name = name.is_a?(Array) ? name.join(' ') : name.to_s @all_commands.find { |cmd| cmd.name == name } end |
#to_h ⇒ {Symbol => String, Boolean} #to_h {|name, value| ... } ⇒ Hash
390 391 392 393 |
# File 'lib/parse-argv.rb', line 390 def to_h(&block) # ensure to return a not frozen copy block ? @rgs.to_h(&block) : Hash[@rgs.to_a] end |
#to_s ⇒ String
Returns the help text of the #current_command
409 410 411 |
# File 'lib/parse-argv.rb', line 409 def to_s current_command.help end |