Class: ParseArgv::Result

Inherits:
Object
  • Object
show all
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

Classes: Command, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

# given there was <format> option defined
result.format?
#=> whether the option was specified
result.format
# String of format option or nil, when not specified


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_commandsArray<Command> (readonly)

Returns all defined commands.

Returns:

  • (Array<Command>)

    all defined commands



268
269
270
# File 'lib/parse-argv.rb', line 268

def all_commands
  @all_commands
end

#current_commandCommand (readonly)

Returns command used for this result.

Returns:

  • (Command)

    command used for this result



273
274
275
# File 'lib/parse-argv.rb', line 273

def current_command
  @current_command
end

#main_commandCommand (readonly)

Returns main command if subcommands are used.

Returns:

  • (Command)

    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.

Examples:

get argument count as a positive integer (or fallback to 10)

result[:count].as(:integer, :positive, default: 10)

Parameters:

  • name (String, Symbol)

    name of the requested argument

Returns:

  • (Value)

    argument value

  • (nil)

    when argument is not defined



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).

Parameters:

  • message (String)

    error message to present

  • code (Integer) (defaults to: 1)

    termination code

Raises:

  • (Error)

    when no error handler was defined

See Also:



321
322
323
324
# File 'lib/parse-argv.rb', line 321

def error!(message, code = 1)
  error = Error.new(current_command, message, 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+.

Overloads:

  • #fetch(name) ⇒ Object

    Will raise an ArgumentError when the requested attribute does not exist.

    Parameters:

    • name (String, Symbol)

      attribute name

    Returns:

    • (Object)

      attribute value

    Raises:

    • (ArgumentError)

      when attribute was not defined

  • #fetch(name, default_value) ⇒ Object

    Returns the +default_value+ when the requested attribute does not exist.

    Parameters:

    • name (String, Symbol)

      attribute name

    • default_value (Object)

      default value to return when attribute not exists

    Returns:

    • (Object)

      attribute value; maybe the +default_value+

  • #fetch(name) {|name| ... } ⇒ Object

    Returns the +block+ result when the requested attribute does not exist.

    Parameters:

    • name (String, Symbol)

      attribute name

    Yield Parameters:

    • name (Symbol)

      attribute name

    Yield Returns:

    • (Object)

      return value

    Returns:

    • (Object)

      attribute value or result of the +block+ if attribute not found



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+.

Parameters:

  • name (String)

Returns:

  • (Command)

    found command

  • (nil)

    when no command was found



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

Overloads:

  • #to_h{Symbol => String, Boolean}

    Transform itself into a Hash containing all arguments.

    Returns:

    • ({Symbol => String, Boolean})

      Hash of all argument name/value pairs

  • #to_h {|name, value| ... } ⇒ Hash

    Returns Hash of all argument key/value pairs.

    Yield Parameters:

    • name (Symbol)

      attribute name

    • value (String, Boolean)

      attribute value

    Yield Returns:

    • (Array(key, value))

      key/value pair to include

    Returns:

    • (Hash)

      Hash of all argument key/value pairs



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_sString

Returns the help text of the #current_command

Returns:

  • (String)

    the help text



409
410
411
# File 'lib/parse-argv.rb', line 409

def to_s
  current_command.help
end