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 ⇒ Boolean
(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.
222 223 224 225 226 |
# File 'lib/arg-parser/argument.rb', line 222 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 ⇒ Boolean Also known as: value_optional?
Whether the keyword argument must be specified with a non-missing value.
200 201 202 |
# File 'lib/arg-parser/argument.rb', line 200 def value_optional @value_optional end |
Instance Method Details
#to_s ⇒ Object
228 229 230 |
# File 'lib/arg-parser/argument.rb', line 228 def to_s "--#{key}".gsub('_', '-') end |
#to_use ⇒ Object
232 233 234 235 236 |
# File 'lib/arg-parser/argument.rb', line 232 def to_use sk = short_key ? "-#{short_key}, " : '' uv = value_optional ? "[#{usage_value}]" : usage_value "#{sk}#{self.to_s} #{uv}" end |