Class: Clio::Usage::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/clio/usage/option.rb

Overview

Commandline Option

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argumentsObject (readonly)

Option arguments.



19
20
21
# File 'lib/clio/usage/option.rb', line 19

def arguments
  @arguments
end

#excludesObject (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

#nameObject (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

#completionObject

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?

Returns:

  • (Boolean)


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

#inspectObject



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

#keyObject

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?

Returns:

  • (Boolean)


66
# File 'lib/clio/usage/option.rb', line 66

def multiple? ; @multiple ; end

#to_sObject



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