Class: DocoptNG::Option
- Inherits:
-
ChildPattern
- Object
- Pattern
- ChildPattern
- DocoptNG::Option
- Defined in:
- lib/docopt_ng/option.rb
Instance Attribute Summary collapse
-
#argcount ⇒ Object
Returns the value of attribute argcount.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
Attributes inherited from ChildPattern
Attributes inherited from Pattern
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(short = nil, long = nil, argcount = 0, value = false) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ Object
- #name ⇒ Object
- #single_match(left) ⇒ Object
Methods inherited from ChildPattern
Methods inherited from Pattern
#==, #dump, #either, #fix, #fix_identities, #fix_repeating_arguments, #to_str
Constructor Details
#initialize(short = nil, long = nil, argcount = 0, value = false) ⇒ Option
Returns a new instance of Option.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/docopt_ng/option.rb', line 8 def initialize(short = nil, long = nil, argcount = 0, value = false) raise RuntimeError unless [0, 1].include? argcount @short = short @long = long @argcount = argcount @value = value @value = if (value == false) && argcount.positive? nil else value end end |
Instance Attribute Details
#argcount ⇒ Object
Returns the value of attribute argcount.
6 7 8 |
# File 'lib/docopt_ng/option.rb', line 6 def argcount @argcount end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
5 6 7 |
# File 'lib/docopt_ng/option.rb', line 5 def long @long end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
5 6 7 |
# File 'lib/docopt_ng/option.rb', line 5 def short @short end |
Class Method Details
.parse(option_description) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/docopt_ng/option.rb', line 23 def self.parse(option_description) short = nil long = nil argcount = 0 value = false , _, description = option_description.strip.partition(' ') = .tr(',', ' ').tr('=', ' ') .split.each do |s| if s.start_with?('--') long = s elsif s.start_with?('-') short = s else argcount = 1 end end if argcount.positive? matched = description.scan(/\[default: (.*)\]/i) value = matched[0][0] if matched.count.positive? end new(short, long, argcount, value) end |
Instance Method Details
#inspect ⇒ Object
59 60 61 |
# File 'lib/docopt_ng/option.rb', line 59 def inspect "Option(#{short}, #{long}, #{argcount}, #{value})" end |
#name ⇒ Object
55 56 57 |
# File 'lib/docopt_ng/option.rb', line 55 def name long || short end |
#single_match(left) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/docopt_ng/option.rb', line 48 def single_match(left) left.each_with_index do |p, n| return [n, p] if name == p.name end [nil, nil] end |