Class: ArgParser::KeywordArgument

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

Overview

An argument that is specified via a keyword prefix; typically used for optional arguments, although Keyword arguments can also be used for mandatory arguments where there is no natural ordering of arguments.

Instance Attribute Summary collapse

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 = {}, &block) ⇒ KeywordArgument

Creates a KeywordArgument, which is an argument that must be specified on a command-line using either a long form key (i.e. –key), or optionally, a short-form key (i.e. -k) should one be defined for this argument.

Parameters:

  • key (Symbol)

    the key that will be used to identify this argument value in the parse results.

  • desc (String)

    the description of this argument, displayed in the generated help screen.

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

    a hash of options that govern the behaviour of this argument.

Options Hash (opts):

  • :required (Boolean)

    whether the keyword argument is a required argument that must appear in the command-line. Defaults to false.

  • :value_optional (Boolean)

    whether the keyword argument can be specified without a value. For example, a keyword argument might be used both as a flag, and to override a default value. Specifying the argument without a value would signify that the option is set, but the default value for the option should be used. Defaults to false (keyword argument cannot be specified without a value).



377
378
379
380
381
# File 'lib/arg-parser/argument.rb', line 377

def initialize(key, desc, opts = {}, &block)
    super
    @required = opts.fetch(:required, false)
    @value_optional = opts.fetch(:value_optional, false)
end

Instance Attribute Details

#value_optionalObject Also known as: value_optional?

Whether the keyword argument must be specified with a non-missing value. The default is false, meaning the keyword argument must be specified together with a value. When this property is set to a non-falsy value (i.e. not nil or false), the keyword argument can be specified either with or without a value (or not at all):

  • If specified with a value, the value will be returned.

  • If specified without a value, the value of this property will be returned.

  • If not specified at all, the default value will be returned.

Returns:

  • (Object)

    If truthy, the argument does not require a value. If the argument is specified but no value is provided, the value of this property will be the argument value.



354
355
356
# File 'lib/arg-parser/argument.rb', line 354

def value_optional
  @value_optional
end

Instance Method Details

#to_sObject



383
384
385
# File 'lib/arg-parser/argument.rb', line 383

def to_s
    "--#{key}".gsub('_', '-')
end

#to_useObject



387
388
389
390
391
# File 'lib/arg-parser/argument.rb', line 387

def to_use
    sk = short_key ? "-#{short_key}, " : ''
    uv = value_optional ? "[#{usage_value}]" : usage_value
    "#{sk}#{self.to_s} #{uv}"
end