Class: ParseOpts::OptSpec
- Inherits:
-
Object
- Object
- ParseOpts::OptSpec
- Defined in:
- lib/parseOpts.rb
Overview
:nodoc: all
Instance Attribute Summary collapse
-
#arg ⇒ Object
readonly
Returns the value of attribute arg.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#multiple ⇒ Object
readonly
Returns the value of attribute multiple.
Instance Method Summary collapse
- #init ⇒ Object
-
#initialize(matches, arg, desc, multiple, default) ⇒ OptSpec
constructor
A new instance of OptSpec.
- #match?(args) ⇒ Boolean
- #name ⇒ Object
- #process(args) ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(matches, arg, desc, multiple, default) ⇒ OptSpec
Returns a new instance of OptSpec.
12 13 14 15 16 17 |
# File 'lib/parseOpts.rb', line 12 def initialize(matches, arg, desc, multiple, default) raise ArgumentError, "Options must start with -" unless matches.all? {|m| m =~ /^-/ } raise ArgumentError, 'Default not allowed unless argument is present' if default && !arg @matches, @arg, @desc, @multiple, @default = matches, arg, desc, multiple, default end |
Instance Attribute Details
#arg ⇒ Object (readonly)
Returns the value of attribute arg.
10 11 12 |
# File 'lib/parseOpts.rb', line 10 def arg @arg end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
10 11 12 |
# File 'lib/parseOpts.rb', line 10 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
10 11 12 |
# File 'lib/parseOpts.rb', line 10 def desc @desc end |
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
10 11 12 |
# File 'lib/parseOpts.rb', line 10 def matches @matches end |
#multiple ⇒ Object (readonly)
Returns the value of attribute multiple.
10 11 12 |
# File 'lib/parseOpts.rb', line 10 def multiple @multiple end |
Instance Method Details
#init ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/parseOpts.rb', line 23 def init @matched = false @value = if @multiple @arg ? [] : 0 else nil end end |
#match?(args) ⇒ Boolean
32 33 34 |
# File 'lib/parseOpts.rb', line 32 def match?(args) (!@matched || @multiple) && (@matches.include?(args[0])) && (!@arg || args.size >= 2) end |
#name ⇒ Object
19 20 21 |
# File 'lib/parseOpts.rb', line 19 def name @matches[0] end |
#process(args) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/parseOpts.rb', line 36 def process(args) args.shift r = @arg ? args.shift : true if @multiple && @arg @value << r elsif @multiple @value += 1 else @value = r end @matched = true end |
#value ⇒ Object
49 50 51 |
# File 'lib/parseOpts.rb', line 49 def value !@matched && @default ? @default : @value end |