Class: ArgParser::RestArgument

Inherits:
ValueArgument show all
Defined in:
lib/arg-parser/argument.rb

Overview

A command-line argument that takes 0 to N values from the command-line.

Instance Attribute Summary

Attributes inherited from ValueArgument

#sensitive, #usage_value, #validation

Attributes inherited from Argument

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

Instance Method Summary collapse

Methods inherited from Argument

to_key

Constructor Details

#initialize(key, desc, opts = {}, &block) ⇒ RestArgument

Creates a new rest argument, which is an argument that consumes all remaining positional argument values.

Parameters:

  • key (Symbol)

    The name that will be used for the accessor used to return this argument value in the parse results.

  • desc (String)

    A description for this argument. Appears in the help output that is generated when the user specifies the –help or /? flags on the command-line.

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

    Contains any options that are desired for this argument.

  • block (Block)

    If supplied, the block passed will be invoked after this argument value has been parsed from the command-line. The block will be called with three arguments: this argument definition, the String value read from the command-line for this argument, and a Hash containing the argument values parsed so far. The return value from the block will be used as the argument value parsed from the command-line for this argument. Blocks are usually used when the value to be returned needs to be converted from a String to some other type.



309
310
311
312
# File 'lib/arg-parser/argument.rb', line 309

def initialize(key, desc, opts = {}, &block)
    super
    @min_values = opts.fetch(:min_values, opts.fetch(:required, true) ? 1 : 0)
end

Instance Method Details

#requiredObject



314
315
316
# File 'lib/arg-parser/argument.rb', line 314

def required
  @min_values > 0
end

#to_sString

Returns the word that will appear in the help display for this argument.

Returns:

  • (String)

    the word that will appear in the help display for this argument.



320
321
322
# File 'lib/arg-parser/argument.rb', line 320

def to_s
    usage_value
end

#to_useString

Returns The string for this argument position in a command-line. usage display.

Returns:

  • (String)

    The string for this argument position in a command-line. usage display.



326
327
328
# File 'lib/arg-parser/argument.rb', line 326

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