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_list_arguments, #to_str
Constructor Details
#initialize(short = nil, long = nil, argcount = 0, value = false) ⇒ Option
Returns a new instance of Option.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/docopt.rb', line 219 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.
217 218 219 |
# File 'lib/docopt.rb', line 217 def argcount @argcount end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
216 217 218 |
# File 'lib/docopt.rb', line 216 def long @long end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
216 217 218 |
# File 'lib/docopt.rb', line 216 def short @short end |
Class Method Details
.parse(option_description) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/docopt.rb', line 234 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 ret = self.new(short, long, argcount, value) return ret end |
Instance Method Details
#inspect ⇒ Object
271 272 273 |
# File 'lib/docopt.rb', line 271 def inspect return "Option(#{self.short}, #{self.long}, #{self.argcount}, #{self.value})" end |
#name ⇒ Object
267 268 269 |
# File 'lib/docopt.rb', line 267 def name return self.long ? self.long : self.short end |
#single_match(left) ⇒ Object
258 259 260 261 262 263 264 265 |
# File 'lib/docopt.rb', line 258 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 |