Class: OptSimple::Option
Overview
An optional parameter, with one or more argument following it. ‘switches’ can be a String or an Array of Strings, and specifies the switches expected on the CL. ‘help’ provides a description of the parameter, and ‘metavar’ will be used in the usage statement. and an optional block can do parameter validation/transformation. If no block is given, then the strings specified (after the leading ‘-’s removed) will be used as keys in the Result and the values set to the arg following the switch on the CL.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Parameter
#block, #metavar, #param_options, #switches
Instance Method Summary collapse
-
#accumulate_opt(val) ⇒ Object
append val to the parameter list.
-
#initialize(switches, help = "", metavar = "ARG", &block) ⇒ Option
constructor
A new instance of Option.
-
#switch_len ⇒ Object
:nodoc:.
-
#switch_str ⇒ Object
:nodoc:.
Methods inherited from Parameter
#error, #help_str, #mandatory?, #names, #set_opt
Constructor Details
#initialize(switches, help = "", metavar = "ARG", &block) ⇒ Option
Returns a new instance of Option.
440 441 442 443 444 445 446 447 448 |
# File 'lib/opt_simple.rb', line 440 def initialize(switches,help="",="ARG",&block) super(switches,help,&block) @metavar = unless block_given? #@block = Proc.new {|arg| @param_options[names.first] = arg} @block = Proc.new {|arg| set_opt arg} end @first_call = true # use this so we can accumulate non-defaults end |
Instance Method Details
#accumulate_opt(val) ⇒ Object
append val to the parameter list
460 461 462 463 464 465 466 467 |
# File 'lib/opt_simple.rb', line 460 def accumulate_opt(val) if @first_call @param_options[names.first] = [val].flatten else @param_options[names.first] << val end @first_call = false end |
#switch_len ⇒ Object
:nodoc:
450 451 452 453 |
# File 'lib/opt_simple.rb', line 450 def switch_len #:nodoc: = @metavar.empty? ? 0 : @metavar.length + 1 super + end |
#switch_str ⇒ Object
:nodoc:
455 456 457 |
# File 'lib/opt_simple.rb', line 455 def switch_str #:nodoc: super + " #{@metavar}" end |