Class: Command
- Inherits:
-
Object
show all
- Defined in:
- lib/command.rb
Direct Known Subclasses
ConfigCommand, ConfigDelete, ConfigGet, ConfigInit, ConfigSet, MyCommand, VzcdnApp, Zone, ZoneAdd, ZoneAudit, ZoneCreate, ZoneDelete, ZoneDiff, ZoneList, ZonePrint, ZonePull, ZonePush, ZoneSetHealthCheck, ZoneUpdate
Defined Under Namespace
Classes: CancelExecution
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, description) ⇒ Command
Returns a new instance of Command.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/command.rb', line 11
def initialize(name, description)
@name = name
@desc = description
@options = [{ short_name: 'h', long_name: 'help', method: :show_help_option, takes_value: false }]
@flows = [ ]
@cellhash = { }
@subcommands = [ ]
@input = { }
init
add_options
create_args
create_flows
end
|
Instance Attribute Details
#desc ⇒ Object
Returns the value of attribute desc.
6
7
8
|
# File 'lib/command.rb', line 6
def desc
@desc
end
|
#name ⇒ Object
Returns the value of attribute name.
6
7
8
|
# File 'lib/command.rb', line 6
def name
@name
end
|
Instance Method Details
#add_arg(name, desc, validator) ⇒ Object
126
127
128
129
130
|
# File 'lib/command.rb', line 126
def add_arg(name, desc, validator)
name = name.to_s
arg = Arg.new(name, desc, validator)
@cellhash[name] = arg
end
|
#add_cmd(cmd_class, name, desc) ⇒ Object
132
133
134
135
136
137
|
# File 'lib/command.rb', line 132
def add_cmd(cmd_class, name, desc)
name = name.to_s
cmd = cmd_class.new(name, desc)
@cellhash[name] = cmd
@subcommands << name
end
|
#add_flow(flow) ⇒ Object
171
172
173
|
# File 'lib/command.rb', line 171
def add_flow(flow)
@flows << flow
end
|
#add_flow_from_usage(usage, *pre_cond) ⇒ Object
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
169
|
# File 'lib/command.rb', line 143
def add_flow_from_usage(usage, *pre_cond)
flow = Flow.new
flow.usage_string = usage
flow.pre_cond = pre_cond
line = usage
re = /\A
( # matches one arg spec
(?<optional> \[)? # turn on 'optional' if we see an open square bracket
<?(?<argname> [[:alnum:]-]+)>? # argname is inside optional angle brackets and consists of alphanumerics and dashes
(=\'(?<default>[^\']+)\')? # default value has form ='value'
(?<repeatable> \s*\.\.\.)? # three dots turn on 'repeatable'
\]? # matching close square bracket
) # end of arg spec
\s* /x index = 0
while(m = re.match(line))
argname = m[:argname]
required = m[:optional].nil?
repeatable = (not m[:repeatable].nil?)
default = m[:default]
index = m.end(0)
line = line[index..-1]
cell = Cell.new(@cellhash[argname], required: required, repeatable: repeatable, default: default)
flow.add(cell)
end
add_flow(flow)
end
|
40
41
42
|
# File 'lib/command.rb', line 40
def add_input(name, value)
@input[name] = value
end
|
#add_option(option) ⇒ Object
44
45
46
|
# File 'lib/command.rb', line 44
def add_option(option)
@options << option
end
|
#add_options ⇒ Object
29
30
|
# File 'lib/command.rb', line 29
def add_options
end
|
#command_name?(str) ⇒ Boolean
139
140
141
|
# File 'lib/command.rb', line 139
def command_name?(str)
@subcommands.include?(str.to_s)
end
|
#create_args ⇒ Object
32
33
|
# File 'lib/command.rb', line 32
def create_args
end
|
#create_flows ⇒ Object
default is to have one flow with no arguments
36
37
38
|
# File 'lib/command.rb', line 36
def create_flows
add_flow(Flow.new)
end
|
#flow_passes_parse(flow, args) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/command.rb', line 175
def flow_passes_parse(flow, args)
desc = "usage \'#{@name} #{flow.usage_string}\'"
result = true
success = flow.parse args, self
if success
if flow.command.nil? && flow.rest.length > 0
result = false
ddputs "#{desc} failed (args=#{args}) due to superfluous arguments (#{flow.rest})"
else
ddputs "#{desc} passed parse, adding to candidate list (rest=#{flow.rest})"
end
else
result = false
ddputs "#{desc} failed to parse arguments (args=#{args}), #{flow.error}"
end
return result
end
|
#flow_passes_preconditions(flow) ⇒ Object
193
194
195
196
197
198
199
200
201
|
# File 'lib/command.rb', line 193
def flow_passes_preconditions(flow)
if flow.pre_cond && flow.pre_cond.length > 0 && (error = send(*flow.pre_cond))
dputs "usage \'#{@name} #{flow.usage_string}\' failed due to precondition failure:#{error}"
false
else
ddputs "usage \'#{@name} #{flow.usage_string}\' passed any precondition"
true
end
end
|
#get_args_used(usage, args_used) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/command.rb', line 114
def get_args_used(usage, args_used)
line = usage
re = /<(?<argname>[[:alnum:]-]+)>/
while m = re.match(line)
argname = m[:argname]
unless args_used.include? argname
args_used << argname
end
line = line[m.end(0)..-1]
end
end
|
#init ⇒ Object
26
27
|
# File 'lib/command.rb', line 26
def init
end
|
#option_help_string(option) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/command.rb', line 86
def option_help_string(option)
validator = option[:takes_value]
result = if validator
case [validator.class]
when [Array]
"one of #{validator}"
when [Regexp]
if option.has_key?(:regexp_description)
option[:regexp_description]
else
"(bug found no regexp_description)"
end
else
"(bug found unknown option validator class #{validator.class})"
end
end
if result && result.length > 0
"=<#{result}>"
else
""
end
end
|
#run(args) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/command.rb', line 203
def run(args)
begin
args = ClOptions.new(self, @options).handle(args)
rescue CancelExecution
return nil
end
if args.class == String
error = args
raise error
end
candidate_flows = @flows.select { |flow|
flow_passes_parse(flow, args)
}
valid_flows = candidate_flows.select { |flow|
flow_passes_preconditions(flow)
}
if valid_flows.length == 1
flow = valid_flows[0]
ddputs "usage \'#{@name} #{flow.usage_string}\' matched, executing"
execute(flow.arghash, flow.rest)
elsif valid_flows.length > 1
puts "BUG FOUND: arguments #{args} match the following flows in class #{self.class}"
valid_flows.each { |flow|
puts " usage \'#{@name} #{flow.usage_string}\'"
}
else
unless args.length == 0
puts "ERROR:no usage of #{@name} recognized or allowed\n\n"
end
show_help
end
end
|
#show_help ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/command.rb', line 48
def show_help
puts "#{@name} - #{@desc}"
valid_flows = @flows.select { |flow|
flow_passes_preconditions(flow)
}
if valid_flows.length == 0
puts "No usage of #{@name} is valid in this context"
valid_flows = @flows
end
puts "Usage:"
args_used = [ ]
valid_flows.each { |flow|
puts " #{@name} #{flow.usage_string}"
get_args_used(flow.usage_string, args_used)
}
if (args_used.length > 0)
puts "Where:"
args_used.each { |name|
arg = @cellhash[name]
choice = ""
if arg.validator.class == String
error, ignore = arg.validate?("asdf", nil)
end
if arg.validator.class == Array
choice = " - value from #{arg.validator}"
elsif arg.validator.class == Hash
choice = " - value from #{arg.validator.keys}"
end
puts " #{arg.name} : #{arg.desc}#{choice}"
}
end
puts "Options:"
@options.each { | o |
puts " -#{o[:short_name]} or --#{o[:long_name]}" + option_help_string(o)
}
end
|
#show_help_option ⇒ Object
109
110
111
112
|
# File 'lib/command.rb', line 109
def show_help_option
show_help
raise CancelExecution
end
|