simple_args_dispatch 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simple_args_dispatch.rb +74 -72
- data/lib/simple_args_dispatch/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 239191fdca3ca4a6da66788cbab985fb6daf28df
|
4
|
+
data.tar.gz: fd25320532cb3205e426f9a47f5e31d135edcfa7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e48df4c18c84d9622ce0318edc04ce0cda9c66396088f037f199583808462fcc312155933231505cdcbe2dc6c5dedd6eea3936c7971054565f36856742c3bca
|
7
|
+
data.tar.gz: cc6c5f20d644ee83f42708b91df0fc0bb1da5e1a1faf43d375a0a818e54db233b9a687a8490610a1864d81f60f64ada2369493a6e6b726f85a1a013ab230a1c3
|
data/lib/simple_args_dispatch.rb
CHANGED
@@ -3,95 +3,97 @@ require 'simple_speaker'
|
|
3
3
|
require 'yaml'
|
4
4
|
|
5
5
|
module SimpleArgsDispatch
|
6
|
+
class Agent
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
# Actions will be in the following format:
|
9
|
+
# {
|
10
|
+
# :help => ['Dispatcher', 'show_available'],
|
11
|
+
# :reconfigure => ['App', 'reconfigure'],
|
12
|
+
# :some_name => {
|
13
|
+
# :some_function => ['SomeName', 'SomeFunction'],
|
14
|
+
# .....
|
15
|
+
# }
|
16
|
+
# end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def initialize(speaker = nil)
|
19
|
+
@speaker = speaker
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
def dispatch(app_name, args, actions, parent = nil, template_dir = '')
|
23
|
+
arg = args.shift
|
24
|
+
actions.each do |k, v|
|
25
|
+
if arg == k.to_s
|
26
|
+
if v.is_a?(Hash)
|
27
|
+
self.dispatch(app_name, args, v, "#{parent} #{arg}", template_dir)
|
28
|
+
else
|
29
|
+
self.launch(app_name, v, args, "#{parent} #{arg}", template_dir)
|
30
|
+
end
|
31
|
+
return
|
29
32
|
end
|
30
|
-
return
|
31
33
|
end
|
32
|
-
|
33
|
-
@speaker.speak_up('Unknown command/option
|
34
|
+
@speaker.speak_up('Unknown command/option
|
34
35
|
|
35
36
|
')
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
def launch(app_name, action, args, parent, template_dir)
|
40
|
-
args = Hash[args.flat_map { |s| s.scan(/--?([^=\s]+)(?:=(.+))?/) }]
|
41
|
-
template_args = parse_template_args(load_template(args['template_name'], template_dir), template_dir)
|
42
|
-
model = Object.const_get(action[0])
|
43
|
-
req_params = model.method(action[1].to_sym).parameters.map { |a| a.reverse! }
|
44
|
-
req_params.each do |param|
|
45
|
-
return self.show_available(app_name, Hash[req_params.map { |k| ["--#{k[0]}=<#{k[0]}>", k[1]] }], parent, ' ') if param[1] == :keyreq && args[param[0].to_s].nil? && template_args[param[0].to_s].nil?
|
37
|
+
self.show_available(app_name, actions, parent)
|
46
38
|
end
|
47
|
-
|
48
|
-
|
39
|
+
|
40
|
+
def launch(app_name, action, args, parent, template_dir)
|
41
|
+
args = Hash[args.flat_map { |s| s.scan(/--?([^=\s]+)(?:=(.+))?/) }]
|
42
|
+
template_args = parse_template_args(load_template(args['template_name'], template_dir), template_dir)
|
43
|
+
model = Object.const_get(action[0])
|
44
|
+
req_params = model.method(action[1].to_sym).parameters.map { |a| a.reverse! }
|
45
|
+
req_params.each do |param|
|
46
|
+
return self.show_available(app_name, Hash[req_params.map { |k| ["--#{k[0]}=<#{k[0]}>", k[1]] }], parent, ' ') if param[1] == :keyreq && args[param[0].to_s].nil? && template_args[param[0].to_s].nil?
|
47
|
+
end
|
48
|
+
$env_flags.each do |k, _|
|
49
|
+
Thread.current[k.to_sym] = (args[k] || template_args[k]).to_i
|
50
|
+
end
|
51
|
+
dameth = model.method(action[1])
|
52
|
+
params = Hash[req_params.map do |k, _|
|
53
|
+
val = args[k.to_s] || template_args[k.to_s]
|
54
|
+
val = eval(val) if val.is_a?(String) && val.match(/^[{\[].*[}\]]$/)
|
55
|
+
[k, val]
|
56
|
+
end].select { |_, v| !v.nil? }
|
57
|
+
@speaker.speak_up("Running with arguments: " + params.map { |a, v| "#{a.to_s}='#{v.to_s}'" }.join(' ')) if $env_flags['debug'] > 0
|
58
|
+
params.empty? ? dameth.call : dameth.call(params)
|
59
|
+
rescue => e
|
60
|
+
@speaker.tell_error(e, "SimpleAgrsDispatch.launch")
|
49
61
|
end
|
50
|
-
dameth = model.method(action[1])
|
51
|
-
params = Hash[req_params.map do |k, _|
|
52
|
-
val = args[k.to_s] || template_args[k.to_s]
|
53
|
-
val = eval(val) if val.is_a?(String) && val.match(/^[{\[].*[}\]]$/)
|
54
|
-
[k, val]
|
55
|
-
end].select { |_, v| !v.nil? }
|
56
|
-
@speaker.speak_up("Running with arguments: " + params.map{|a, v| "#{a.to_s}='#{v.to_s}'"}.join(' ')) if $env_flags['debug'] > 0
|
57
|
-
params.empty? ? dameth.call : dameth.call(params)
|
58
|
-
rescue => e
|
59
|
-
@speaker.tell_error(e, "SimpleAgrsDispatch.launch")
|
60
|
-
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
63
|
+
def load_template(template_name, template_dir)
|
64
|
+
if template_name.to_s != '' && File.exist?(template_dir + '/' + "#{template_name}.yml")
|
65
|
+
return YAML.load_file(template_dir + '/' + "#{template_name}.yml")
|
66
|
+
end
|
67
|
+
{}
|
68
|
+
rescue
|
69
|
+
{}
|
65
70
|
end
|
66
|
-
{}
|
67
|
-
rescue
|
68
|
-
{}
|
69
|
-
end
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
def new_line
|
73
|
+
'---------------------------------------------------------'
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
def parse_template_args(template, template_dir)
|
77
|
+
template.keys.each do |k|
|
78
|
+
if k.to_s == 'load_template'
|
79
|
+
template[k] = [template[k]] if template[k].is_a?(String)
|
80
|
+
template[k].each do |t|
|
81
|
+
template.merge!(parse_template_args(load_template(t.to_s, template_dir), template_dir))
|
82
|
+
end
|
83
|
+
template.delete(k)
|
84
|
+
elsif template[k].is_a?(Hash)
|
85
|
+
template[k] = parse_template_args(template[k], template_dir)
|
81
86
|
end
|
82
|
-
template.delete(k)
|
83
|
-
elsif template[k].is_a?(Hash)
|
84
|
-
template[k] = parse_template_args(template[k], template_dir)
|
85
87
|
end
|
88
|
+
template
|
86
89
|
end
|
87
|
-
template
|
88
|
-
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
def show_available(app_name, available, prepend = nil, join='|', separator = new_line, extra_info = '')
|
92
|
+
@speaker.speak_up("Usage: #{app_name} #{prepend + ' ' if prepend}#{available.map { |k, v| "#{k.to_s}#{'(optional)' if v == :opt}" }.join(join)}")
|
93
|
+
if extra_info.to_s != ''
|
94
|
+
@speaker.speak_up(separator)
|
95
|
+
@speaker.speak_up(extra_info)
|
96
|
+
end
|
95
97
|
end
|
96
98
|
end
|
97
99
|
end
|