class RCTCLI
Public Class Methods
rct_cli()
click to toggle source
# File lib/rct/rct_cli.rb, line 45 def self.rct_cli method = ARGV.shift if (method == nil || method.empty?) RCT.bad_invocation("no CLI class/method given!") end method =~ /([^\.]*)\.(.*)/ class_name = $1 method_name = $2 RCT.log(DEBUG, "Requested [#{method}]") RCT.log(DEBUG, "CLI class: #{class_name}, method: #{method_name}") if (!class_name) RCT.error("No class specified for CLI operation") exit(1) end if (!method_name) RCT.error("No method specified for CLI operation") exit(1) end obj = Object::const_get(class_name).new() begin cli_info = obj.send('cli') rescue Exception => e puts e RCT.error("#{class_name} does not support CLI operations") exit(1) end if (method_name == "HELP") description = obj.send('description') show_help(description, cli_info) end method_info = cli_info[method_name] if (method_info == nil) RCT.error("#{class_name}.#{method_name} not available") exit(1) end RCT.parse_options(method_info['required'], true) RCT.parse_options(method_info['optional'], false) response = obj.send(method_name) { $HTTP.handle_request() } RCT.log(INFO, response) cli_output = RCT.sget(CLI_OUTPUT) if (cli_output != nil) puts cli_output else puts response puts response.body end end
show_help(description, cli_info)
click to toggle source
# File lib/rct/rct_cli.rb, line 108 def self.show_help(description, cli_info) puts if (description != nil) puts description puts end cli_info.each_key { |key| method_info = cli_info[key] description = method_info['description'] req = method_info['required'] opt = method_info['optional'] puts puts "#{key}: #{description}" if (req != nil && req.length > 0) puts " Required arguments:" req.each { |key,arg| puts " #{arg[0]} (#{arg[1]}) : #{arg[2]}" } end if (opt != nil && opt.length > 0) puts " Optional arguments:" opt.each { |key,arg| puts " #{arg[0]} (#{arg[1]}) : #{arg[2]}" } end } exit(0) end