Class: RCTCLI
- Inherits:
-
Object
- Object
- RCTCLI
- Defined in:
- lib/rct/rct_cli.rb
Overview
Provides access to rct client methods as a CLI.
In order to be accessible via CLI, a client class must implement a ‘cli’ method which provides info about the CLI methods and their arguments.
Client methods can provide friendly output in CLI mode by setting CLI_OUTPUT to the desired text. If it is not set, the raw response body is shown.
Class Method Summary collapse
-
.rct_cli ⇒ Object
—————————————————————————- CLI entry point.
-
.show_help(description, cli_info) ⇒ Object
—————————————————————————- Show autogenerated help based on cli_info.
Class Method Details
.rct_cli ⇒ Object
CLI entry point.
ARGV must contain string “Class.operation”. We’ll instantiate ‘Class’ and execute CLI operation ‘operation’. This operation must be one described by the ‘cli’ inspection method.
The exception is if ‘operation’ is ‘HELP’, in which case auto-generated help output is shown.
45 46 47 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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.(method_info['required'], true) RCT.(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) ⇒ Object
Show autogenerated help based on cli_info
108 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 135 136 137 138 139 140 141 142 |
# 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 |