Module: SimpleArgsDispatch
- Defined in:
- lib/simple_args_dispatch.rb,
lib/simple_args_dispatch/version.rb
Constant Summary collapse
- VERSION =
"0.2.4"
Class Method Summary collapse
-
.dispatch(app_name, args, actions, parent = nil, template_dir = '') ⇒ Object
Actions will be in the following format: { :help => [‘Dispatcher’, ‘show_available’], :reconfigure => [‘App’, ‘reconfigure’], :some_name => { :some_function => [‘SomeName’, ‘SomeFunction’], .…. } end.
- .launch(app_name, action, args, parent, template_dir) ⇒ Object
- .load_template(template_name, template_dir) ⇒ Object
- .new_line ⇒ Object
- .parse_template_args(template, template_dir) ⇒ Object
- .show_available(app_name, available, prepend = nil, join = '|', separator = new_line, extra_info = '') ⇒ Object
- .speaker ⇒ Object
Class Method Details
.dispatch(app_name, args, actions, parent = nil, template_dir = '') ⇒ Object
Actions will be in the following format:
{
:help => ['Dispatcher', 'show_available'],
:reconfigure => ['App', 'reconfigure'],
:some_name => {
:some_function => ['SomeName', 'SomeFunction'],
.....
}
end
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/simple_args_dispatch.rb', line 17 def self.dispatch(app_name, args, actions, parent = nil, template_dir = '') arg = args.shift actions.each do |k, v| if arg == k.to_s if v.is_a?(Hash) self.dispatch(app_name, args, v, "#{parent} #{arg}", template_dir) else self.launch(app_name, v, args, "#{parent} #{arg}", template_dir) end return end end speaker.speak_up('Unknown command/option ') self.show_available(app_name, actions, parent) end |
.launch(app_name, action, args, parent, template_dir) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/simple_args_dispatch.rb', line 35 def self.launch(app_name, action, args, parent, template_dir) args = Hash[args.flat_map { |s| s.scan(/--?([^=\s]+)(?:=(.+))?/) }] template_args = parse_template_args(load_template(args['template_name'], template_dir), template_dir) model = Object.const_get(action[0]) req_params = model.method(action[1].to_sym).parameters.map { |a| a.reverse! } req_params.each do |param| 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? end $env_flags.each do |k,_| $env_flags[k] = (args[k] || template_args[k]).to_i end dameth = model.method(action[1]) params = Hash[req_params.map do |k, _| val = args[k.to_s] || template_args[k.to_s] val = eval(val) if val.is_a?(String) && val.match(/^[{\[].*[}\]]$/) [k, val] end].select { |_, v| !v.nil? } speaker.speak_up("Running with arguments: " + params.map{|a, v| "#{a.to_s}='#{v.to_s}'"}.join(' ')) if $env_flags['debug'] > 0 params.empty? ? dameth.call : dameth.call(params) rescue => e speaker.tell_error(e, "SimpleAgrsDispatch.launch") end |
.load_template(template_name, template_dir) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/simple_args_dispatch.rb', line 58 def self.load_template(template_name, template_dir) if template_name.to_s != '' && File.exist?(template_dir + '/' + "#{template_name}.yml") return YAML.load_file(template_dir + '/' + "#{template_name}.yml") end {} rescue {} end |
.new_line ⇒ Object
67 68 69 |
# File 'lib/simple_args_dispatch.rb', line 67 def self.new_line '---------------------------------------------------------' end |
.parse_template_args(template, template_dir) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple_args_dispatch.rb', line 71 def self.parse_template_args(template, template_dir) template.keys.each do |k| if k.to_s == 'load_template' && template[k].is_a?(String) template.merge!(load_template(template[k].to_s, template_dir)) template.delete(k) elsif template[k].is_a?(Hash) template[k] = parse_template_args(template[k], template_dir) end end template end |
.show_available(app_name, available, prepend = nil, join = '|', separator = new_line, extra_info = '') ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/simple_args_dispatch.rb', line 83 def self.show_available(app_name, available, prepend = nil, join='|', separator = new_line, extra_info = '') speaker.speak_up("Usage: #{app_name} #{prepend + ' ' if prepend}#{available.map { |k, v| "#{k.to_s}#{'(optional)' if v == :opt}" }.join(join)}") if extra_info.to_s != '' speaker.speak_up(separator) speaker.speak_up(extra_info) end end |
.speaker ⇒ Object
91 92 93 |
# File 'lib/simple_args_dispatch.rb', line 91 def self.speaker @speaker ||= SimpleSpeaker::Speaker.new end |