Class: A4Tools::BuiltinCommand
- Inherits:
-
Object
- Object
- A4Tools::BuiltinCommand
- Defined in:
- lib/net_shell/builtin_command.rb
Class Attribute Summary collapse
-
.category_text ⇒ Object
readonly
Returns the value of attribute category_text.
-
.command ⇒ Object
Returns the value of attribute command.
-
.description_text ⇒ Object
readonly
Returns the value of attribute description_text.
-
.filepath ⇒ Object
Returns the value of attribute filepath.
-
.help_text ⇒ Object
readonly
Returns the value of attribute help_text.
-
.is_user_defined ⇒ Object
readonly
Returns the value of attribute is_user_defined.
-
.opts ⇒ Object
readonly
Returns the value of attribute opts.
-
.run_block ⇒ Object
readonly
Returns the value of attribute run_block.
-
.sha1 ⇒ Object
Returns the value of attribute sha1.
-
.tab_completors ⇒ Object
readonly
Returns the value of attribute tab_completors.
-
.usage_text ⇒ Object
readonly
Returns the value of attribute usage_text.
-
.validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#error ⇒ Object
Returns the value of attribute error.
-
#input ⇒ Object
Returns the value of attribute input.
-
#output ⇒ Object
Returns the value of attribute output.
-
#params ⇒ Object
Returns the value of attribute params.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#shell ⇒ Object
Returns the value of attribute shell.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.category(category) ⇒ Object
Command category.
-
.description(description) ⇒ Object
Summary of command One-line description of command for use in help.
-
.help(help) ⇒ Object
Help text Supplemental help added to end of –help.
-
.load_command(file, userdefined = false) ⇒ Object
Load from a file (don’t call this directly).
-
.opt(key, description, params = {}) ⇒ Object
Defines a command line argument.
-
.run(&block) ⇒ Object
Actual code block to execute when the command is run.
- .setup ⇒ Object
-
.tab(index = nil, &tab_completor) ⇒ Object
Tab completion Return an array of possible values.
-
.usage(usage) ⇒ Object
Command line usage Added to end of usage argment.
-
.user_defined ⇒ Object
Identify a command as user-defined (called automatically).
-
.validate(&validator) ⇒ Object
Command line validation Return true to indicate command line is valid.
Instance Method Summary collapse
- #all_opts ⇒ Object
- #block_call(&block) ⇒ Object
- #category ⇒ Object
- #client ⇒ Object
- #command ⇒ Object
- #console_print(s) ⇒ Object
- #console_puts(s) ⇒ Object
- #deployment ⇒ Object
- #description ⇒ Object
- #educate ⇒ Object
- #execute(input, output, error, args) ⇒ Object
- #global_opts ⇒ Object
- #help ⇒ Object
-
#initialize(shell, input = nil, output = nil, error = nil) ⇒ BuiltinCommand
constructor
don’t override these unless you really know what you’re doing and have a good reason.
- #kaiconfig ⇒ Object
- #parse(args) ⇒ Object
- #parser ⇒ Object
- #perform ⇒ Object
- #print(s = "") ⇒ Object
- #puts(s = "") ⇒ Object
- #qa ⇒ Object
- #show_error(msg) ⇒ Object
- #show_exception(exception) ⇒ Object
- #sym_for_param(param) ⇒ Object
- #tab ⇒ Object
- #usage ⇒ Object
- #usage_msg ⇒ Object
- #user_defined? ⇒ Boolean
-
#usher ⇒ Object
convenience methods you might want to be aware of.
- #usherm ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(shell, input = nil, output = nil, error = nil) ⇒ BuiltinCommand
don’t override these unless you really know what you’re doing and have a good reason
143 144 145 146 147 148 149 150 |
# File 'lib/net_shell/builtin_command.rb', line 143 def initialize(shell, input=nil, output=nil, error=nil) @shell = shell @parser = parser @input = input || StandardInput.new @output = output || StandardOutput.new @error = error || StandardOutput.new end |
Class Attribute Details
.category_text ⇒ Object (readonly)
Returns the value of attribute category_text.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def category_text @category_text end |
.command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/net_shell/builtin_command.rb', line 8 def command @command end |
.description_text ⇒ Object (readonly)
Returns the value of attribute description_text.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def description_text @description_text end |
.filepath ⇒ Object
Returns the value of attribute filepath.
8 9 10 |
# File 'lib/net_shell/builtin_command.rb', line 8 def filepath @filepath end |
.help_text ⇒ Object (readonly)
Returns the value of attribute help_text.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def help_text @help_text end |
.is_user_defined ⇒ Object (readonly)
Returns the value of attribute is_user_defined.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def is_user_defined @is_user_defined end |
.opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def opts @opts end |
.run_block ⇒ Object (readonly)
Returns the value of attribute run_block.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def run_block @run_block end |
.sha1 ⇒ Object
Returns the value of attribute sha1.
8 9 10 |
# File 'lib/net_shell/builtin_command.rb', line 8 def sha1 @sha1 end |
.tab_completors ⇒ Object (readonly)
Returns the value of attribute tab_completors.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def tab_completors @tab_completors end |
.usage_text ⇒ Object (readonly)
Returns the value of attribute usage_text.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def usage_text @usage_text end |
.validator ⇒ Object (readonly)
Returns the value of attribute validator.
7 8 9 |
# File 'lib/net_shell/builtin_command.rb', line 7 def validator @validator end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def args @args end |
#error ⇒ Object
Returns the value of attribute error.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def error @error end |
#input ⇒ Object
Returns the value of attribute input.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def input @input end |
#output ⇒ Object
Returns the value of attribute output.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def output @output end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def params @params end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
4 5 6 |
# File 'lib/net_shell/builtin_command.rb', line 4 def result @result end |
#shell ⇒ Object
Returns the value of attribute shell.
3 4 5 |
# File 'lib/net_shell/builtin_command.rb', line 3 def shell @shell end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/net_shell/builtin_command.rb', line 4 def status @status end |
Class Method Details
.category(category) ⇒ Object
Command category. Used to sort items in help.
23 24 25 |
# File 'lib/net_shell/builtin_command.rb', line 23 def category(category) @category_text = category end |
.description(description) ⇒ Object
Summary of command One-line description of command for use in help
29 30 31 |
# File 'lib/net_shell/builtin_command.rb', line 29 def description(description) @description_text = description end |
.help(help) ⇒ Object
Help text Supplemental help added to end of –help.
41 42 43 |
# File 'lib/net_shell/builtin_command.rb', line 41 def help(help) @help_text = help end |
.load_command(file, userdefined = false) ⇒ Object
Load from a file (don’t call this directly)
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/net_shell/builtin_command.rb', line 77 def load_command(file, userdefined=false) cmdname = File.basename(file.to_s, ".rb") subclass = Class.new(A4Tools::BuiltinCommand) {} contents = IO.read(file) subclass.setup subclass.command = cmdname subclass.filepath = file subclass.sha1 = Digest::SHA1.hexdigest contents subclass.user_defined if userdefined subclass.class_eval( contents, file ) subclass end |
.opt(key, description, params = {}) ⇒ Object
Defines a command line argument. Identical syntax to Trollop.
opt :name, "Supplies a name", :type => :string
66 67 68 |
# File 'lib/net_shell/builtin_command.rb', line 66 def opt(key, description, params={}) @opts.push({ key:key, description:description, params:params }) end |
.run(&block) ⇒ Object
Actual code block to execute when the command is run
71 72 73 |
# File 'lib/net_shell/builtin_command.rb', line 71 def run(&block) @run_block = block end |
.setup ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/net_shell/builtin_command.rb', line 10 def setup @description_text = "" @usage_text = "" @help_text = "" @category_text = "General" @validator = proc { true } @tab_completors = { default: proc { [] } } @run_block = proc { "" } @opts = [] end |
.tab(index = nil, &tab_completor) ⇒ Object
Tab completion Return an array of possible values. You are not responsible for filtering these values for those matching the current argument. If an index is supplied, the block will only be invoked for that argument.
56 57 58 59 60 61 62 |
# File 'lib/net_shell/builtin_command.rb', line 56 def tab(index=nil, &tab_completor) if index.nil? then @tab_completors[:default] = tab_completor else @tab_completors[index] = tab_completor end end |
.usage(usage) ⇒ Object
Command line usage Added to end of usage argment. e.g. return “arg1 arg2” to get a string like “usage: my_command arg1 arg2”
35 36 37 |
# File 'lib/net_shell/builtin_command.rb', line 35 def usage(usage) @usage_text = usage end |
.user_defined ⇒ Object
Identify a command as user-defined (called automatically)
92 93 94 |
# File 'lib/net_shell/builtin_command.rb', line 92 def user_defined @is_user_defined = true end |
.validate(&validator) ⇒ Object
Command line validation Return true to indicate command line is valid. Return false to generate error and return usage help. Return a string to generate error and display the returned string.
49 50 51 |
# File 'lib/net_shell/builtin_command.rb', line 49 def validate(&validator) @validator = validator end |
Instance Method Details
#all_opts ⇒ Object
248 249 250 |
# File 'lib/net_shell/builtin_command.rb', line 248 def all_opts global_opts + self.class.opts end |
#block_call(&block) ⇒ Object
180 181 182 183 |
# File 'lib/net_shell/builtin_command.rb', line 180 def block_call(&block) self.class.send(:define_method, :__block_call, &block) self.class.send(:instance_method, :__block_call).bind(self).call end |
#category ⇒ Object
152 153 154 |
# File 'lib/net_shell/builtin_command.rb', line 152 def category self.class.category_text end |
#client ⇒ Object
112 113 114 |
# File 'lib/net_shell/builtin_command.rb', line 112 def client @shell.active_client end |
#command ⇒ Object
156 157 158 |
# File 'lib/net_shell/builtin_command.rb', line 156 def command self.class.command end |
#console_print(s) ⇒ Object
308 309 310 |
# File 'lib/net_shell/builtin_command.rb', line 308 def console_print(s) $stdout.print s.gsub("\n","\r\n") end |
#console_puts(s) ⇒ Object
304 305 306 |
# File 'lib/net_shell/builtin_command.rb', line 304 def console_puts(s) $stdout.puts s.gsub("\n","\r\n") end |
#deployment ⇒ Object
116 117 118 |
# File 'lib/net_shell/builtin_command.rb', line 116 def deployment @shell.client(:deployment) end |
#description ⇒ Object
160 161 162 |
# File 'lib/net_shell/builtin_command.rb', line 160 def description self.class.description_text end |
#educate ⇒ Object
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/net_shell/builtin_command.rb', line 285 def educate buffer = PipeBuffer.new @parser.educate buffer intro_line = "#{command.style(:command)}: #{description}" userdef_line = user_defined? ? "User defined".style(:userdefined) : nil top_section = [intro_line, category.style(:category), userdef_line ].compact.join("\n") [top_section+"\n", usage_msg+"\n", buffer.buffer, help].compact.join("\n") end |
#execute(input, output, error, args) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/net_shell/builtin_command.rb', line 224 def execute(input, output, error, args) @input = input @output = output @error = error parse(args) r = perform r = [r, 0] unless r.is_a? Array @result = r[0] || "" @status = r[1] txt = @result.to_s unless txt.empty? then txt += "\n" unless txt.end_with? "\n" @output.write txt end @status end |
#global_opts ⇒ Object
244 245 246 |
# File 'lib/net_shell/builtin_command.rb', line 244 def global_opts [ { key: :help, description:"Show help", params:{} } ] end |
#help ⇒ Object
168 169 170 |
# File 'lib/net_shell/builtin_command.rb', line 168 def help self.class.help_text end |
#kaiconfig ⇒ Object
124 125 126 |
# File 'lib/net_shell/builtin_command.rb', line 124 def kaiconfig @shell.client(:kaiconfig) end |
#parse(args) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/net_shell/builtin_command.rb', line 260 def parse(args) @realargs = Array.new(args) begin @params = @parser.parse(args) rescue Trollop::HelpNeeded @params = {help:true} rescue Trollop::VersionNeeded @params = {help:true} rescue Trollop::CommandlineError @params = {help:true} end @args = @parser.leftovers @args = args if @args.nil? or @args.empty? end |
#parser ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/net_shell/builtin_command.rb', line 252 def parser Trollop::Parser.new(all_opts) do |opts| opts.each do |option| opt option[:key], option[:description], option[:params] end end end |
#perform ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/net_shell/builtin_command.rb', line 205 def perform return [educate, 1] if params[:help] errmsg = validate return show_error(errmsg) if errmsg.is_a? String return show_error(usage_msg) if errmsg.nil? or errmsg == false begin block_call(&self.class.run_block) rescue Interrupt @error.write "Killed\n" return ["", 1] rescue SystemExit # allow commands to call exit raise rescue Exception => exception return show_exception(exception) end end |
#print(s = "") ⇒ Object
296 297 298 |
# File 'lib/net_shell/builtin_command.rb', line 296 def print(s="") @output.print s end |
#puts(s = "") ⇒ Object
300 301 302 |
# File 'lib/net_shell/builtin_command.rb', line 300 def puts(s="") @output.puts s end |
#qa ⇒ Object
120 121 122 |
# File 'lib/net_shell/builtin_command.rb', line 120 def qa @shell.client(:qa) end |
#show_error(msg) ⇒ Object
128 129 130 131 |
# File 'lib/net_shell/builtin_command.rb', line 128 def show_error(msg) @error.write "!!".style(:error) + " #{args[0].style(:command)}: #{msg}\n" return "", 1 end |
#show_exception(exception) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/net_shell/builtin_command.rb', line 133 def show_exception(exception) @error.write "\n" @error.write "!!".style(:error) + "#{command} crashed: #{exception.class.to_s} #{exception.}\n" @error.write "Stack trace: #{exception.backtrace.map {|l| " #{l}\n"}.join}" "" end |
#sym_for_param(param) ⇒ Object
185 186 187 |
# File 'lib/net_shell/builtin_command.rb', line 185 def sym_for_param(param) param[2..-1].to_sym rescue "" end |
#tab ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/net_shell/builtin_command.rb', line 189 def tab # only include --command-arguments if we already have a dash param_mix = [] param_mix = all_opts.map { |opt| "--" + opt[:key].to_s.gsub("_","-") } if @realargs.last[0] == '-' key = if @realargs.length >= 2 and self.class.tab_completors.has_key? sym_for_param(@realargs[-2]) sym_for_param(@realargs[-2]) elsif self.class.tab_completors.has_key?(args.length-2) args.length - 2 else :default end param_mix + block_call(&(self.class.tab_completors[key])) end |
#usage ⇒ Object
164 165 166 |
# File 'lib/net_shell/builtin_command.rb', line 164 def usage self.class.usage_text end |
#usage_msg ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/net_shell/builtin_command.rb', line 276 def usage_msg u = usage if usage.is_a? Array then "usage:\n" + (u.map { |line| "\t#{command} [flags] #{line}" }).join("\n") else "usage: #{command} [flags] #{usage}" end end |
#user_defined? ⇒ Boolean
176 177 178 |
# File 'lib/net_shell/builtin_command.rb', line 176 def user_defined? self.class.is_user_defined end |
#usher ⇒ Object
convenience methods you might want to be aware of
101 102 103 104 105 106 |
# File 'lib/net_shell/builtin_command.rb', line 101 def usher if @shell.shared[:usher].nil? then @shell.shared[:usher] = A4Tools::UsherClient.new() end @shell.shared[:usher] end |
#usherm ⇒ Object
108 109 110 |
# File 'lib/net_shell/builtin_command.rb', line 108 def usherm @shell.client(:usherm) end |
#validate ⇒ Object
172 173 174 |
# File 'lib/net_shell/builtin_command.rb', line 172 def validate block_call(&self.class.validator) end |