class Command
Command
is the main class used to define commands in Zabcon which are then inserted into the global singleton class CommandList
Attributes
aliases[R]
argument_processor[R]
flags[R]
help_tag[R]
path[R]
required_args[R]
str[R]
valid_args[R]
Public Class Methods
new(path)
click to toggle source
# File libs/command_tree.rb, line 250 def initialize(path) raise "Path must be an array" if path.class!=Array @path=path @cmd_method=nil @valid_args=@required_args=[] @aliases=[] @flags={} @result_type=nil #TODO Can the argument processor stuff be cleaned up? # @argument_processor=method(:default_processor) #The argument processor is nil by default. #The method call_arg_processor will call the tokenizer and the #argument processor if it is not nil. #Otherwise a default method will be called which will check the #current parameters list for validity. @argument_processor=nil @tokenizer=ExpressionTokenizerHash @tokenizer_method=nil @help_tag=nil @depreciated=nil end
Public Instance Methods
add_alias(name)
click to toggle source
Adds an alias name for the current command
# File libs/command_tree.rb, line 308 def add_alias(name) @aliases<<name.split2 end
alias_total()
click to toggle source
How many alias' are there?
# File libs/command_tree.rb, line 313 def alias_total @aliases.length end
arg_processor(method=nil,&block)
click to toggle source
# File libs/command_tree.rb, line 350 def arg_processor(method=nil,&block) raise CommandError.new("arg_processor cannot be passed a method and a block") if !method.nil? && !block.nil? if !method.nil? @argument_processor=method else @argument_processor=block end end
call_arg_processor(parameters)
click to toggle source
# File libs/command_tree.rb, line 451 def call_arg_processor(parameters) debug(6,:msg=>"parameters",:var=>"\"#{parameters.inspect}\"") check_parameters(parameters) @arguments=Arguments.new(parameters, @flags) debug(6,:var=>@arguments) @arguments end
call_tokenizer(parameters)
click to toggle source
# File libs/command_tree.rb, line 442 def call_tokenizer(parameters) debug(6,:msg=>"parameters",:var=>"\"#{parameters.inspect}\"") debug(7,:msg=>"Using tokenizer", :var=>@tokenizer.to_s) tokenized_parameters=@tokenizer.new(parameters) tokenized_parameters=@tokenizer_method.call(tokenized_parameters) if @tokenizer_method debug(7,:msg=>"Tokenized Parameters",:var=>tokenized_parameters) tokenized_parameters.parse end
check_parameters(parameters)
click to toggle source
# File libs/command_tree.rb, line 408 def check_parameters(parameters) return if !parameters.is_a?(Hash) if !@valid_args.empty? args_keys=parameters.keys invalid_args=args_keys-@valid_args if @valid_args raise ParameterError.new("Invalid parameters: "+invalid_args.join(", "), :retry=>true) if !invalid_args.empty? required_args=@required_args.reject{|i| i.class==Array } required_or_args=@required_args.reject{|i| i.class!=Array } missing_args=[] missing_args=required_args-args_keys required_or_args.delete_if do |i| count=i.length missing_args<<i if (i-args_keys).count==count end if !missing_args.empty? msg=missing_args.map do |i| if i.class==Array "(#{i.join(" | ")})" else i end end.join(", ") raise ParameterError.new("Missing required arguments: #{msg}",:retry=>true) end end end
command_name()
click to toggle source
# File libs/command_tree.rb, line 291 def command_name @path.join(" ") end
default_show(cols)
click to toggle source
# File libs/command_tree.rb, line 341 def default_show(cols) raise "Cols must be an array" if cols.class!=Array @flags[:default_cols]=cols end
deprecate_function(new_func)
click to toggle source
# File libs/command_tree.rb, line 274 def deprecate_function(new_func) #probe the stack to find the deprecated function name caller[0]=~/`(.*?)'/ function=$1 #probe the stack again to find the command definition caller[1]=~/(.*):(\d+).*/ path=$1 line_num=$2 warn("Command definition Warning") warn(" \"#{function}\" is depreciated and may be removed in future versions") warn(" use \"#{new_func}\". Command: \"#{command_name}\", line number #{line_num}") warn(" Path: #{path}") warn(" Fixing the command definition will remove this warning.") end
depreciated(str)
click to toggle source
# File libs/command_tree.rb, line 346 def depreciated(str) @depreciated=str end
execute(parameters)
click to toggle source
# File libs/command_tree.rb, line 465 def execute(parameters) def set_result_message(msg) @response.message=msg end def set_result_type(type) @response.type=type end # def output(params) # @response.data<<params # end raise LoopError.new("Loop detected, Command.execute called more than 3 times") if caller.grep(caller[0]).length>3 @response=Response.new @response.type=@result_type if !@result_type.nil? puts @depreciated if !@depreciated.nil? if !@flags.nil? && @flags[:login_required] && !server.connected? raise LoginRequired.new("\"#{@path.join(" ")}\" requires an active login") end @response.data=@cmd_method.call(parameters) retval=@response #ensure @result is empty for our next use @response=nil retval end
generate_alias(index)
click to toggle source
# File libs/command_tree.rb, line 317 def generate_alias(index) raise "Index out of bounds 0 >= i < N, i=#{index} N=#{alias_total}" if index<0 || index>=alias_total new_alias=self.dup new_alias.instance_variable_set("@path",@aliases[index]) new_alias.instance_variable_set("@aliases",[]) new_alias end
print?()
click to toggle source
# File libs/command_tree.rb, line 295 def print? if @flags.nil? false else @flags[:print_output]==true end end
result_type(type)
click to toggle source
Sets the symbold describing the result type. May be used by the print processor
as a hint for printing the output.
# File libs/command_tree.rb, line 461 def result_type(type) @result_type=type end
set_arg_processor(method)
click to toggle source
# File libs/command_tree.rb, line 360 def set_arg_processor(method) deprecate_function("arg_processor") @argument_processor=method end
set_flag(flag,val=nil)
click to toggle source
Valid flags:
:login_required - command requires a valid login :print_output - the output of the command will be passed to the print processor :array_params - Only process the parameters as an array
# File libs/command_tree.rb, line 399 def set_flag(flag,val=nil) case flag.class.to_s when "Symbol" flag=val.nil? ? {flag=>true} : {flag=>val} end @flags.merge!(flag) end
set_help_tag(sym)
click to toggle source
# File libs/command_tree.rb, line 365 def set_help_tag(sym) @help_tag=sym end
set_method(&cmd_method)
click to toggle source
# File libs/command_tree.rb, line 303 def set_method(&cmd_method) @cmd_method=cmd_method end
set_result_message(msg)
click to toggle source
# File libs/command_tree.rb, line 466 def set_result_message(msg) @response.message=msg end
set_result_type(type)
click to toggle source
# File libs/command_tree.rb, line 470 def set_result_type(type) @response.type=type end
set_tokenizer(tokenizer)
click to toggle source
# File libs/command_tree.rb, line 369 def set_tokenizer(tokenizer) deprecate_function("tokenizer") @tokenizer=tokenizer end
set_valid_args(*args)
click to toggle source
accepts an array of valid arguments
# File libs/command_tree.rb, line 337 def set_valid_args(*args) @valid_args=@valid_args|args end
tokenizer(tokenizer=nil,&tokenizer_method)
click to toggle source
# File libs/command_tree.rb, line 374 def tokenizer(tokenizer=nil,&tokenizer_method) #Check to see that tokenizer is a descendant of Tokenizer #Returns False or nil if for negative results if !tokenizer.nil? && (tokenizer <= Tokenizer) @tokenizer=tokenizer else p tokenizer raise CommandError.new("Tokenizer must be a descendant of the Tokenizer Class") end if tokenizer_method if tokenizer_method.arity!=1 CommandError.new("Tokenizer blocks require an arity of one") end @tokenizer_method=tokenizer_method end end
Private Instance Methods
global_vars()
click to toggle source
# File libs/command_tree.rb, line 505 def global_vars GlobalVars.instance end
parameter_error(msg)
click to toggle source
# File libs/command_tree.rb, line 497 def parameter_error(msg) raise ParameterError.new(msg) end
server()
click to toggle source
# File libs/command_tree.rb, line 501 def server ZabbixServer.instance end