Class: ArgParser::KeywordArgument
- Inherits:
-
ValueArgument
- Object
- Argument
- ValueArgument
- ArgParser::KeywordArgument
- 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
-
#value_optional ⇒ Object
(also: #value_optional?)
Whether the keyword argument must be specified with a non-missing value.
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
-
#initialize(key, desc, opts = {}, &block) ⇒ KeywordArgument
constructor
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.
- #to_s ⇒ Object
- #to_use ⇒ Object
Methods inherited from Argument
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.
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_optional ⇒ Object 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.
354 355 356 |
# File 'lib/arg-parser/argument.rb', line 354 def value_optional @value_optional end |
Instance Method Details
#to_s ⇒ Object
383 384 385 |
# File 'lib/arg-parser/argument.rb', line 383 def to_s "--#{key}".gsub('_', '-') end |
#to_use ⇒ Object
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 |