Class: Clio::Usage::Option
- Inherits:
-
Object
- Object
- Clio::Usage::Option
- Defined in:
- lib/clio/usage/option.rb
Overview
Commandline Option
Instance Attribute Summary collapse
-
#aliases(*names) ⇒ Object
readonly
Specify aliases for the option.
-
#arguments ⇒ Object
readonly
Option arguments.
-
#excludes ⇒ Object
readonly
Option (names) that are mutually exlusive to this option.
-
#help(string = nil) ⇒ Object
readonly
Help text for this option.
-
#multiple(bool = nil) ⇒ Object
readonly
Specify if the option be used multiple times.
-
#name ⇒ Object
readonly
Option name.
Instance Method Summary collapse
- #===(other) ⇒ Object
-
#arg(slot, help = nil) ⇒ Object
Argument shorthand.
-
#argument(name, &block) ⇒ Object
Assign an argument to the option.
-
#completion ⇒ Object
Tab completion.
-
#flag? ⇒ Boolean
Is this option a boolean flag?.
-
#initialize(name, &block) ⇒ Option
constructor
New Option.
- #initialize_copy(o) ⇒ Object
- #inspect ⇒ Object
-
#key ⇒ Object
Same as
name
but given as a symbol. -
#multiple? ⇒ Boolean
Can this option occur multiple times in the command line?.
- #to_s ⇒ Object
-
#xor(*opts) ⇒ Object
Specify mutually exclusive options.
Constructor Details
#initialize(name, &block) ⇒ Option
New Option. def initialize(name, parent=nil, &block)
38 39 40 41 42 43 44 45 46 47 48 |
# 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 |
Instance Attribute Details
#aliases(*names) ⇒ Object (readonly)
Specify aliases for the option.
16 17 18 |
# File 'lib/clio/usage/option.rb', line 16 def aliases @aliases end |
#arguments ⇒ Object (readonly)
Option arguments.
19 20 21 |
# File 'lib/clio/usage/option.rb', line 19 def arguments @arguments end |
#excludes ⇒ Object (readonly)
Option (names) that are mutually exlusive to this option.
26 27 28 |
# File 'lib/clio/usage/option.rb', line 26 def excludes @excludes end |
#help(string = nil) ⇒ Object (readonly)
Help text for this option.
34 35 36 |
# File 'lib/clio/usage/option.rb', line 34 def help @help end |
#multiple(bool = nil) ⇒ Object (readonly)
Specify if the option be used multiple times.
22 23 24 |
# File 'lib/clio/usage/option.rb', line 22 def multiple @multiple end |
#name ⇒ Object (readonly)
Option name.
13 14 15 |
# File 'lib/clio/usage/option.rb', line 13 def name @name end |
Instance Method Details
#===(other) ⇒ Object
171 172 173 174 175 176 |
# 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) ⇒ Object
Argument shorthand.
arg('PIN', 'pin number')
137 138 139 |
# File 'lib/clio/usage/option.rb', line 137 def arg(slot, help=nil) argument(slot).help(help) end |
#argument(name, &block) ⇒ Object
Assign an argument to the option.
87 88 89 90 91 92 |
# 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 ⇒ Object
Tab completion.
126 127 128 |
# File 'lib/clio/usage/option.rb', line 126 def completion arguments.collect{|c| c.type} end |
#flag? ⇒ Boolean
Is this option a boolean flag?
84 |
# File 'lib/clio/usage/option.rb', line 84 def flag?; @arguments.empty?; end |
#initialize_copy(o) ⇒ Object
51 52 53 54 55 56 57 |
# 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 ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# 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 ⇒ Object
Same as name
but given as a symbol.
60 61 62 |
# File 'lib/clio/usage/option.rb', line 60 def key name.to_sym end |
#multiple? ⇒ Boolean
Can this option occur multiple times in the command line?
66 |
# File 'lib/clio/usage/option.rb', line 66 def multiple? ; @multiple ; end |
#to_s ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# 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) ⇒ Object
Specify mutually exclusive options.
110 111 112 |
# File 'lib/clio/usage/option.rb', line 110 def xor(*opts) @exclusive.concat(opts) end |