Class: ArgParser::PositionalArgument

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

Overview

An argument that is set by position on the command-line. PositionalArguments do not require a –key to be specified before the argument value; they are typically used when there are a small number of mandatory arguments.

Positional arguments still have a key that is used to identify the parsed argument value in the results Struct. As such, it is not an error for a positional argument to be specified with its key - its just not mandatory for the key to be provided.

Instance Attribute Summary

Attributes inherited from ValueArgument

#sensitive, #usage_value, #validation

Attributes inherited from Argument

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

Instance Method Summary collapse

Methods inherited from Argument

lookup, register, to_key

Constructor Details

#initialize(key, desc, opts = {}) {|val, arg, hsh| ... } ⇒ PositionalArgument

Creates a new positional argument, which is an argument value that may be specified without a keyword, in which case it is matched to the available positional arguments by its position on the command-line.

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.

Yields:

  • (val, arg, hsh)

    If supplied, the block passed will be invoked after this argument value has been parsed from the command-line. Blocks are usually used when the value to be returned needs to be converted from a String to some other type.

Yield Parameters:

  • val (String)

    the String value read from the command-line for this argument

  • arg (PositionalArgument)

    this argument definition

  • hsh (Hash)

    a Hash containing the argument values parsed so far.

Yield Returns:

  • (Object)

    the return value from the block will be used as the argument value parsed from the command-line for this argument.



215
216
217
218
# File 'lib/arg-parser/argument.rb', line 215

def initialize(key, desc, opts = {}, &block)
    super
    @required = opts.fetch(:required, !opts.has_key?(:default))
end

Instance Method Details

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



222
223
224
# File 'lib/arg-parser/argument.rb', line 222

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.



228
229
230
# File 'lib/arg-parser/argument.rb', line 228

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