Class: OptSimple::Parameter
- Inherits:
-
Object
- Object
- OptSimple::Parameter
- Defined in:
- lib/opt_simple.rb
Overview
The base class for command line parameter objects from which Flag, Option and Argument are derived. Users will probably only use the an ‘error method’ and the ‘set_opt’ utility funciton.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#metavar ⇒ Object
readonly
Returns the value of attribute metavar.
-
#param_options ⇒ Object
readonly
Returns the value of attribute param_options.
-
#switches ⇒ Object
Returns the value of attribute switches.
Instance Method Summary collapse
-
#error(string) ⇒ Object
A shortcut for raising an OptSimple::Error exception.
-
#help_str(switch_len) ⇒ Object
a single line that will be put in the overall usage string.
-
#initialize(switches, help = "", &block) ⇒ Parameter
constructor
‘switches’ can be a String or an Array of Strings, and specifies the switches expected on the CL.
-
#mandatory? ⇒ Boolean
is it mandatory to see this parameter on the command line? Returns false unless overidden.
-
#names ⇒ Object
returns a list of the switches without the leading ‘-’s.
-
#set_opt(val) ⇒ Object
A utility function that sets all the names to the specified value in the param_options data structure.
-
#switch_len ⇒ Object
:nodoc:.
-
#switch_str ⇒ Object
:nodoc:.
Constructor Details
#initialize(switches, help = "", &block) ⇒ Parameter
‘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 an optional block can do parameter validation/transformation.
334 335 336 337 338 339 340 |
# File 'lib/opt_simple.rb', line 334 def initialize(switches,help="",&block) self.switches = switches @help = help @block = block @param_options = OptSimple::Result.new @param_options.add_alias(self.names) end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
329 330 331 |
# File 'lib/opt_simple.rb', line 329 def block @block end |
#metavar ⇒ Object (readonly)
Returns the value of attribute metavar.
329 330 331 |
# File 'lib/opt_simple.rb', line 329 def @metavar end |
#param_options ⇒ Object (readonly)
Returns the value of attribute param_options.
329 330 331 |
# File 'lib/opt_simple.rb', line 329 def @param_options end |
#switches ⇒ Object
Returns the value of attribute switches.
329 330 331 |
# File 'lib/opt_simple.rb', line 329 def switches @switches end |
Instance Method Details
#error(string) ⇒ Object
A shortcut for raising an OptSimple::Error exception
383 384 385 |
# File 'lib/opt_simple.rb', line 383 def error(string) raise OptSimple::Error.new string end |
#help_str(switch_len) ⇒ Object
a single line that will be put in the overall usage string
378 379 380 |
# File 'lib/opt_simple.rb', line 378 def help_str(switch_len) " %-#{switch_len}s" % self.switch_str + " \t#{@help}" end |
#mandatory? ⇒ Boolean
is it mandatory to see this parameter on the command line? Returns false unless overidden.
395 |
# File 'lib/opt_simple.rb', line 395 def mandatory?; false; end |
#names ⇒ Object
returns a list of the switches without the leading ‘-’s
348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/opt_simple.rb', line 348 def names names = [] switches.each do |s| st = s.sub(/^-+/,'') names << st << st.to_sym if st.include?('-') st2 = st.gsub('-','_') names << st2 << st2.to_sym end end names end |
#set_opt(val) ⇒ Object
A utility function that sets all the names to the specified value in the param_options data structure.
389 390 391 392 |
# File 'lib/opt_simple.rb', line 389 def set_opt val # all the other values are aliased, so we only need to set the first @param_options[names.first] = val end |
#switch_len ⇒ Object
:nodoc:
361 362 363 |
# File 'lib/opt_simple.rb', line 361 def switch_len #:nodoc: @switches.join(', ').length end |
#switch_str ⇒ Object
:nodoc:
365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/opt_simple.rb', line 365 def switch_str #:nodoc: short_parms = @switches.find_all {|st| st.start_with?('-') and st.length == 2 and st[1] != '-'} long_parms = @switches.find_all {|st| st.start_with?('--') or (st.start_with?('-') and st.length > 2)} other_parms = @switches.find_all {|st| not st.start_with?('-')} sh_str = short_parms.empty? ? " " * 4 : short_parms.first long_str = long_parms.join(', ') + other_parms.join(', ') sh_str << ', ' unless sh_str =~/^\s+$/ or long_str.empty? sh_str + long_str end |