Class: Docopt::Option
- Inherits:
-
ChildPattern
- Object
- Pattern
- ChildPattern
- Docopt::Option
- Defined in:
- lib/docopt.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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/docopt.rb', line 242 def initialize(short=nil, long=nil, argcount=0, value=false) unless [0, 1].include? argcount raise RuntimeError end @short, @long = short, long @argcount, @value = argcount, value if value == false and argcount > 0 @value = nil else @value = value end end |
Instance Attribute Details
#argcount ⇒ Object
Returns the value of attribute argcount.
240 241 242 |
# File 'lib/docopt.rb', line 240 def argcount @argcount end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
239 240 241 |
# File 'lib/docopt.rb', line 239 def long @long end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
239 240 241 |
# File 'lib/docopt.rb', line 239 def short @short end |
Class Method Details
.parse(option_description) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/docopt.rb', line 257 def self.parse(option_description) short, long, argcount, value = nil, nil, 0, false , _, description = option_description.strip.partition(' ') = .gsub(',', ' ').gsub('=', ' ') for s in .split if s.start_with?('--') long = s elsif s.start_with?('-') short = s else argcount = 1 end end if argcount > 0 matched = description.scan(/\[default: (.*)\]/i) value = matched[0][0] if matched.count > 0 end new(short, long, argcount, value) end |
Instance Method Details
#inspect ⇒ Object
292 293 294 |
# File 'lib/docopt.rb', line 292 def inspect return "Option(#{self.short}, #{self.long}, #{self.argcount}, #{self.value})" end |
#name ⇒ Object
288 289 290 |
# File 'lib/docopt.rb', line 288 def name return self.long ? self.long : self.short end |
#single_match(left) ⇒ Object
279 280 281 282 283 284 285 286 |
# File 'lib/docopt.rb', line 279 def single_match(left) left.each_with_index do |p, n| if self.name == p.name return [n, p] end end return [nil, nil] end |