Class: UsageMod::ArgumentList
- Inherits:
-
Object
- Object
- UsageMod::ArgumentList
- Defined in:
- lib/Usage.rb
Overview
This class holds the parsed representation of the usage string. It holds the options (both required and optional) and the arguments (both required and optional)
Instance Attribute Summary collapse
-
#infinite_argument ⇒ Object
Returns the value of attribute infinite_argument.
-
#optional_arguments ⇒ Object
Returns the value of attribute optional_arguments.
-
#options ⇒ Object
Returns the value of attribute options.
-
#required_arguments ⇒ Object
Returns the value of attribute required_arguments.
Instance Method Summary collapse
-
#get_next_arg(index) ⇒ Object
get the next argument.
-
#has_infinite_arg ⇒ Object
is there an infinite argument and is it last.
-
#initialize ⇒ ArgumentList
constructor
A new instance of ArgumentList.
-
#last_arg ⇒ Object
give the last argument in the list.
-
#lookup_option(name) ⇒ Object
lookup an option and return Option object based on the name.
-
#process ⇒ Object
check to make sure the argument list is correct?.
-
#push_arg(aArgument) ⇒ Object
add and argument to the list.
-
#push_option(aOption) ⇒ Object
add an option to the list.
-
#required_options ⇒ Object
returns the options that are required.
-
#update_with_long_names ⇒ Object
update the options_hash with their long name equivalents.
Constructor Details
#initialize ⇒ ArgumentList
Returns a new instance of ArgumentList.
305 306 307 308 309 310 |
# File 'lib/Usage.rb', line 305 def initialize() @required_arguments = [] @optional_arguments = [] @options_hash = {} @infinite_argument = nil end |
Instance Attribute Details
#infinite_argument ⇒ Object
Returns the value of attribute infinite_argument.
303 304 305 |
# File 'lib/Usage.rb', line 303 def infinite_argument @infinite_argument end |
#optional_arguments ⇒ Object
Returns the value of attribute optional_arguments.
303 304 305 |
# File 'lib/Usage.rb', line 303 def optional_arguments @optional_arguments end |
#options ⇒ Object
Returns the value of attribute options.
303 304 305 |
# File 'lib/Usage.rb', line 303 def @options end |
#required_arguments ⇒ Object
Returns the value of attribute required_arguments.
303 304 305 |
# File 'lib/Usage.rb', line 303 def required_arguments @required_arguments end |
Instance Method Details
#get_next_arg(index) ⇒ Object
get the next argument
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/Usage.rb', line 329 def get_next_arg( index ) if index < @required_arguments.size then arg = @required_arguments[index] elsif index < (@required_arguments.size + @optional_arguments.size) then arg = @optional_arguments[index-@required_arguments.size] else arg = nil end arg end |
#has_infinite_arg ⇒ Object
is there an infinite argument and is it last
322 323 324 |
# File 'lib/Usage.rb', line 322 def has_infinite_arg @required_arguments.size > 0 && last_arg.infinite end |
#last_arg ⇒ Object
give the last argument in the list
315 316 317 |
# File 'lib/Usage.rb', line 315 def last_arg @required_arguments[@required_arguments.size - 1] end |
#lookup_option(name) ⇒ Object
lookup an option and return Option object based on the name
378 379 380 |
# File 'lib/Usage.rb', line 378 def lookup_option(name) @options_hash[name] end |
#process ⇒ Object
check to make sure the argument list is correct?
395 396 397 398 399 400 401 402 403 |
# File 'lib/Usage.rb', line 395 def process @required_arguments.each_with_index do |arg, i| if arg.infinite then if i != required_arguments.size - 1 then raise InfiniteArgNotLast.new("infinite argument #{arg.name} is not last") end end end end |
#push_arg(aArgument) ⇒ Object
add and argument to the list
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/Usage.rb', line 344 def push_arg(aArgument) if aArgument.optional then $TRACE.debug 5, "push_arg: pushing optional argument #{aArgument.inspect}" @optional_arguments.push(aArgument) else # FIXME: raise "Required arguments cannot follow optional arguments" if @optional_arguments.size > 0 $TRACE.debug 5, "push_arg: pushing required argument #{aArgument.inspect}" @required_arguments.push(aArgument) end $TRACE.debug 5, "push_arg: @optional_arguments.size = #{@optional_arguments.size}" $TRACE.debug 5, "push_arg: @optional_arguments = #{@optional_arguments.inspect}" end |
#push_option(aOption) ⇒ Object
add an option to the list
360 361 362 |
# File 'lib/Usage.rb', line 360 def push_option(aOption) @options_hash[aOption.name] = aOption end |
#required_options ⇒ Object
returns the options that are required
385 386 387 388 389 390 |
# File 'lib/Usage.rb', line 385 def ar = @options_hash.values $TRACE.debug 9, "options_hash = #{@options_hash.inspect}" $TRACE.debug 9, "ar = #{ar.inspect}" ar.select{|opt| opt.is_required} end |
#update_with_long_names ⇒ Object
update the options_hash with their long name equivalents
367 368 369 370 371 372 373 |
# File 'lib/Usage.rb', line 367 def update_with_long_names $TRACE.debug 9, "before long names: options_hash = #{@options_hash.inspect}" @options_hash.values.each do |option| @options_hash[option.long_name] = option if option.long_name end $TRACE.debug 9, "after long names: options_hash = #{@options_hash.inspect}" end |