Class: Command
- Inherits:
-
Object
show all
- Defined in:
- lib/command.rb
Direct Known Subclasses
ConfigCommand, ConfigDelete, ConfigGet, ConfigInit, ConfigSet, MyCommand, VzcdnApp, Zone, ZoneAdd, ZoneCreate, ZoneDelete, ZoneList, ZonePrint, ZonePull, ZonePush
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
92
93
94
95
96
|
# File 'lib/command.rb', line 92
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
98
99
100
101
102
103
|
# File 'lib/command.rb', line 98
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
136
137
138
|
# File 'lib/command.rb', line 136
def add_flow(flow)
@flows << flow
end
|
#add_flow_from_usage(usage) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/command.rb', line 109
def add_flow_from_usage(usage)
flow = Flow.new
flow.usage_string = usage
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
105
106
107
|
# File 'lib/command.rb', line 105
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
|
#get_args_used(usage, args_used) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/command.rb', line 53
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
|
#run(args) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/command.rb', line 140
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
@flows.each { |flow|
error, rest = flow.parse args, self
if error.nil?
execute(flow.arghash, rest)
return
end
}
puts "ERROR:no usage of #{@name} recognized"
puts ""
show_help
end
|
#show_help ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/command.rb', line 65
def show_help
puts "#{@name} - #{@desc}"
puts "Usage:"
args_used = [ ]
@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
end
|
#show_help_option ⇒ Object
48
49
50
51
|
# File 'lib/command.rb', line 48
def show_help_option
show_help
raise CancelExecution
end
|