Class: OptionParser::Switch
- Inherits:
-
Object
- Object
- OptionParser::Switch
- Defined in:
- lib/optparse.rb
Overview
Individual switch class. Not important to the user.
Defined within Switch are several Switch-derived classes: NoArgument, RequiredArgument, etc.
Direct Known Subclasses
NoArgument, OptionalArgument, PlacedArgument, RequiredArgument
Defined Under Namespace
Classes: NoArgument, OptionalArgument, PlacedArgument, RequiredArgument
Instance Attribute Summary collapse
-
#arg ⇒ Object
readonly
Returns the value of attribute arg.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#conv ⇒ Object
readonly
Returns the value of attribute conv.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
Class Method Summary collapse
-
.guess(arg) ⇒ Object
Guesses argument style from
arg
. - .incompatible_argument_styles(arg, t) ⇒ Object
- .pattern ⇒ Object
Instance Method Summary collapse
-
#add_banner(to) ⇒ Object
:nodoc:.
-
#initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) ⇒ Switch
constructor
A new instance of Switch.
-
#match_nonswitch?(str) ⇒ Boolean
:nodoc:.
-
#summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")) ⇒ Object
Produces the summary text.
-
#switch_name ⇒ Object
Main name of the switch.
Constructor Details
#initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) ⇒ Switch
Returns a new instance of Switch.
311 312 313 314 315 316 317 |
# File 'lib/optparse.rb', line 311 def initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) raise if Array === pattern @pattern, @conv, @short, @long, @arg, @desc, @block = pattern, conv, short, long, arg, desc, block end |
Instance Attribute Details
#arg ⇒ Object (readonly)
Returns the value of attribute arg
282 283 284 |
# File 'lib/optparse.rb', line 282 def arg @arg end |
#block ⇒ Object (readonly)
Returns the value of attribute block
282 283 284 |
# File 'lib/optparse.rb', line 282 def block @block end |
#conv ⇒ Object (readonly)
Returns the value of attribute conv
282 283 284 |
# File 'lib/optparse.rb', line 282 def conv @conv end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc
282 283 284 |
# File 'lib/optparse.rb', line 282 def desc @desc end |
#long ⇒ Object (readonly)
Returns the value of attribute long
282 283 284 |
# File 'lib/optparse.rb', line 282 def long @long end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern
282 283 284 |
# File 'lib/optparse.rb', line 282 def pattern @pattern end |
#short ⇒ Object (readonly)
Returns the value of attribute short
282 283 284 |
# File 'lib/optparse.rb', line 282 def short @short end |
Class Method Details
.guess(arg) ⇒ Object
Guesses argument style from arg
. Returns corresponding OptionParser::Switch class (OptionalArgument, etc.).
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/optparse.rb', line 288 def self.guess(arg) case arg when "" t = self when /\A=?\[/ t = Switch::OptionalArgument when /\A\s+\[/ t = Switch::PlacedArgument else t = Switch::RequiredArgument end self >= t or incompatible_argument_styles(arg, t) t end |
.incompatible_argument_styles(arg, t) ⇒ Object
303 304 305 |
# File 'lib/optparse.rb', line 303 def self.incompatible_argument_styles(arg, t) raise ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}" end |
.pattern ⇒ Object
307 308 309 |
# File 'lib/optparse.rb', line 307 def self.pattern NilClass end |
Instance Method Details
#add_banner(to) ⇒ Object
:nodoc:
401 402 403 404 405 406 407 |
# File 'lib/optparse.rb', line 401 def (to) # :nodoc: unless @short or @long s = desc.join to << " [" + s + "]..." unless s.empty? end to end |
#match_nonswitch?(str) ⇒ Boolean
:nodoc:
409 410 411 |
# File 'lib/optparse.rb', line 409 def match_nonswitch?(str) # :nodoc: @pattern =~ str unless @short or @long end |
#summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")) ⇒ Object
Produces the summary text. Each line of the summary is yielded to the block (without newline).
sdone
-
Already summarized short style options keyed hash.
ldone
-
Already summarized long style options keyed hash.
width
-
Width of left side (option part). In other words, the right side (description part) starts after
width
columns. max
-
Maximum width of left side -> the options are filled within
max
columns. indent
-
Prefix string indents all summarized lines.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/optparse.rb', line 370 def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "") sopts, lopts, s = [], [], nil @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long return if sopts.empty? and lopts.empty? # completely hidden left = [sopts.join(', ')] right = desc.dup while s = lopts.shift l = left[-1].length + s.length l += arg.length if left.size == 1 && arg l < max or sopts.empty? or left << '' left[-1] << if left[-1].empty? then ' ' * 4 else ', ' end << s end left[0] << arg if arg mlen = left.collect {|s| s.length}.max.to_i while mlen > width and l = left.shift mlen = left.collect {|s| s.length}.max.to_i if l.length == mlen yield(indent + l) end while begin l = left.shift; r = right.shift; l or r end l = l.to_s.ljust(width) + ' ' + r if r and !r.empty? yield(indent + l) end self end |
#switch_name ⇒ Object
Main name of the switch.
416 417 418 |
# File 'lib/optparse.rb', line 416 def switch_name (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '') end |