class Clio::Usage::Option
Commandline Option¶ ↑
Attributes
aliases[R]
Alternate names for this option.
arguments[R]
Option arguments.
excludes[R]
Option (names) that are mutually exlusive to this option.
help[R]
Help text for this option.
multiple[R]
Option can be used more that once.
name[R]
Option name.
Public Class Methods
new(name, &block)
click to toggle source
New Option.
def initialize(name, parent=nil, &block)
# File lib/clio/usage/option.rb, line 38 def initialize(name, &block) @name = clean_name(name) #@parent = parent @aliases = [] @arguments = [] @multiple = false #@greedy = false @exclude = [] @help = '' instance_eval(&block) if block end
Public Instance Methods
===(other)
click to toggle source
# File lib/clio/usage/option.rb, line 171 def ===(other) other = clean_key(other) return true if clean_key(key) == other return true if aliases.include?(other) return false end
arg(slot, help=nil)
click to toggle source
Argument shorthand.
arg('PIN', 'pin number')
# File lib/clio/usage/option.rb, line 137 def arg(slot, help=nil) argument(slot).help(help) end
argument(name, &block)
click to toggle source
Assign an argument to the option.
# File lib/clio/usage/option.rb, line 87 def argument(name, &block) arg = Argument.new(name) #, self) arg.instance_eval(&block) if block @arguments << arg arg end
completion()
click to toggle source
Tab completion.
# File lib/clio/usage/option.rb, line 126 def completion arguments.collect{|c| c.type} end
flag?()
click to toggle source
Is this option a boolean flag?
# File lib/clio/usage/option.rb, line 84 def flag?; @arguments.empty?; end
initialize_copy(o)
click to toggle source
# File lib/clio/usage/option.rb, line 51 def initialize_copy(o) @name = o.name.dup @aliases = o.aliases.dup #@multiple = o.multiple @exclude = o.exclude.dup @help = o.help.dup end
inspect()
click to toggle source
# File lib/clio/usage/option.rb, line 72 def inspect to_s #s = "[--#{key}" #s << "*" if multiple #s << "=" + arguments.join(',') unless arguments.empty? #s << " " + aliases.collect{ |a| "-#{a}" } unless aliases.empty? ##s << " @excludes=#{@excludes.inspect}" unless @excludes.empty? #s << "]" #s end
key()
click to toggle source
Same as name
but given as a symbol.
# File lib/clio/usage/option.rb, line 60 def key name.to_sym end
multiple?()
click to toggle source
Can this option occur multiple times in the command line?
# File lib/clio/usage/option.rb, line 66 def multiple? ; @multiple ; end
to_s()
click to toggle source
# File lib/clio/usage/option.rb, line 142 def to_s tiny = aliases.select do |a| a.to_s.size == 1 end tiny.unshift(name) if name.size == 1 long = aliases.select do |a| a.to_s.size > 1 end long.unshift(name) if name.size > 1 tiny = tiny.collect{ |l| "-#{l}" } long = long.collect{ |w| "--#{w}" } if tiny.empty? opts = [ ' ', *long ] else opts = tiny + long end unless arguments.empty? args = arguments.collect{ |a| a.to_s.sub(/^[<]/,'').sub(/[>]$/,'') } opts.last << "=" + args.join(',') end opts.join(' ') end
xor(*opts)
click to toggle source
Specify mutually exclusive options.
# File lib/clio/usage/option.rb, line 110 def xor(*opts) @exclusive.concat(opts) end
Private Instance Methods
clean_key(key)
click to toggle source
# File lib/clio/usage/option.rb, line 185 def clean_key(key) clean_name(key).to_sym end
clean_name(name)
click to toggle source
# File lib/clio/usage/option.rb, line 190 def clean_name(name) name = name.to_s name = name.gsub(/^[-]+/, '') name.chomp('?') end
flag_key?(key)
click to toggle source
# File lib/clio/usage/option.rb, line 197 def flag_key?(key) name = key.to_s return key[-1,1] == '?' end
option_key(key)
click to toggle source
# File lib/clio/usage/option.rb, line 203 def option_key(key) k = clean_key(key).to_s if k.size == 1 "-#{k}".to_sym else "--#{k}".to_sym end end