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

lookup, register, 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.

Options Hash (opts):

  • :min_values (Fixnum)

    The minimum number of rest values that must be supplied. Defaults to 1 if the RestArgument is required, or 0 if it is not.



472
473
474
475
476
# File 'lib/arg-parser/argument.rb', line 472

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

Instance Method Details

#requiredObject



478
479
480
# File 'lib/arg-parser/argument.rb', line 478

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.



484
485
486
# File 'lib/arg-parser/argument.rb', line 484

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.



490
491
492
# File 'lib/arg-parser/argument.rb', line 490

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