Class: Docopt::Option
- Inherits:
-
Object
- Object
- Docopt::Option
- Defined in:
- lib/docopt.rb
Instance Attribute Summary collapse
-
#argcount ⇒ Object
readonly
Returns the value of attribute argcount.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #getopt ⇒ Object
-
#initialize(parse) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ Object
- #set_value(val) ⇒ Object
- #symbols ⇒ Object
- #synonyms ⇒ Object
Constructor Details
#initialize(parse) ⇒ Option
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/docopt.rb', line 11 def initialize parse @argcount = 0 , _, description = parse.strip.partition(' ') = .sub(',', ' ').sub('=', ' ') for s in .split if s.start_with? '--' @long = s elsif s.start_with? '-' @short = s else @argcount = 1 end end if @argcount == 1 matched = description.scan(/\[default: (.*)\]/)[0] @value = matched ? matched[0] : nil end end |
Instance Attribute Details
#argcount ⇒ Object (readonly)
Returns the value of attribute argcount.
9 10 11 |
# File 'lib/docopt.rb', line 9 def argcount @argcount end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
9 10 11 |
# File 'lib/docopt.rb', line 9 def long @long end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
9 10 11 |
# File 'lib/docopt.rb', line 9 def short @short end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
9 10 11 |
# File 'lib/docopt.rb', line 9 def value @value end |
Instance Method Details
#==(other) ⇒ Object
58 59 60 |
# File 'lib/docopt.rb', line 58 def == other self.inspect == other.inspect end |
#getopt ⇒ Object
50 51 52 |
# File 'lib/docopt.rb', line 50 def getopt [long, short, argcount].compact end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/docopt.rb', line 54 def inspect "#<Docopt::Option short: #{short}, long: #{long}, argcount: #{argcount}, value: #{value}>" end |
#set_value(val) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/docopt.rb', line 32 def set_value val if argcount.zero? @value = true else @value = val end end |
#symbols ⇒ Object
44 45 46 47 48 |
# File 'lib/docopt.rb', line 44 def symbols [short, long].compact.map do |name| name.gsub(/^-+/, '').to_sym end end |
#synonyms ⇒ Object
40 41 42 |
# File 'lib/docopt.rb', line 40 def synonyms ([short, long] + symbols).compact end |