Class: Command
Overview
Command
This is used to store a tree of Commands by name Like a hash, but with a few extra features
knife
node
list
edit
environment
list
edit
Allows you to navigate up and down the tree.
Defined Under Namespace
Classes: AmbiguousCommand, NoMoreSubCommands, NoMoreSuppliedCommands, NotFoundCommand
Constant Summary
Constants included from Dbg
Dbg::DefaultIO, Dbg::DefaultLevel
Instance Attribute Summary collapse
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#cmd_short ⇒ Object
Returns the value of attribute cmd_short.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#level ⇒ Object
Returns the value of attribute level.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#root ⇒ Object
Returns the value of attribute root.
-
#source ⇒ Object
Returns the value of attribute source.
-
#sub_commands ⇒ Object
Returns the value of attribute sub_commands.
Instance Method Summary collapse
-
#add_command(commands) ⇒ Object
Add a list of commands, recursing down the tree.
-
#add_subcmd(command) ⇒ Object
Add a sub Command to this Command.
-
#command_lookup ⇒ Object
Recurse back down the Command tree and return the full command.
-
#has_no_sub_commands? ⇒ Boolean
Test for existence of a sub Command.
-
#has_sub_command?(command) ⇒ Boolean
Test for existence of a sub Command.
-
#initialize(command, argopts = {}) ⇒ Command
constructor
A new instance of Command.
-
#pp ⇒ Object
Print current command as a tree, recurse through sub commands.
-
#pp_single ⇒ Object
Print the current command on a single line if it’s the last in the tree, otherwise move down.
-
#process_lookup(commands) ⇒ Object
Recursively lookup a list of commands.
-
#sub_command(command) ⇒ Object
Return a sub Comamnd by name.
-
#sub_command_find(command) ⇒ Object
Find a sub Comamnd via substr.
-
#to_s ⇒ Object
return the command string.
Methods included from Dbg
create, #dbg, #debug_logger, debug_logger, replace
Constructor Details
#initialize(command, argopts = {}) ⇒ Command
Returns a new instance of Command.
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 |
# File 'lib/kknife/command.rb', line 52 def initialize( command, argopts = {} ) @cmd = command opts = { :cmd_short => nil, :source => nil, :level => 0, :sub_commands => {}, :parent => nil, :root => self, :controller => nil }.merge argopts @cmd_short = opts[:cmd_short] @source = opts[:source] @parent = opts[:parent] @level = opts[:level] @sub_commands = opts[:sub_commands] @root = opts[:root] @controller = opts[:controller] # this could just return the root's controller. @controller = @root.controller if not @root.nil? and @controller.nil? raise "require controller" if @controller.nil? @level = @parent.level + 1 unless @parent.nil? dbg 'test' debug_logger.debug( 'Command init' ) end |
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def cmd @cmd end |
#cmd_short ⇒ Object
Returns the value of attribute cmd_short.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def cmd_short @cmd_short end |
#controller ⇒ Object
Returns the value of attribute controller.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def controller @controller end |
#level ⇒ Object
Returns the value of attribute level.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def level @level end |
#parent ⇒ Object
Returns the value of attribute parent.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def parent @parent end |
#root ⇒ Object
Returns the value of attribute root.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def root @root end |
#source ⇒ Object
Returns the value of attribute source.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def source @source end |
#sub_commands ⇒ Object
Returns the value of attribute sub_commands.
50 51 52 |
# File 'lib/kknife/command.rb', line 50 def sub_commands @sub_commands end |
Instance Method Details
#add_command(commands) ⇒ Object
Add a list of commands, recursing down the tree
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kknife/command.rb', line 144 def add_command( commands ) first = commands.shift dbg "first", first if has_sub_command? first dbg "first exists", first end sub_cmd = add_subcmd first sub_cmd.add_command( commands ) unless commands.empty? end |
#add_subcmd(command) ⇒ Object
Add a sub Command to this Command
90 91 92 93 94 95 96 |
# File 'lib/kknife/command.rb', line 90 def add_subcmd( command ) unless has_sub_command? command dbg 'add_subcmd', command, cmd @sub_commands[command] = Command.new( command, :parent => self, :root => @root ) end @sub_commands[command] end |
#command_lookup ⇒ Object
Recurse back down the Command tree and return the full command
117 118 119 120 |
# File 'lib/kknife/command.rb', line 117 def command_lookup return [ @cmd ] + @parent.command_lookup unless @parent.nil? [ @cmd ] end |
#has_no_sub_commands? ⇒ Boolean
Test for existence of a sub Command
123 124 125 |
# File 'lib/kknife/command.rb', line 123 def has_no_sub_commands? @sub_commands.empty? end |
#has_sub_command?(command) ⇒ Boolean
Test for existence of a sub Command
128 129 130 |
# File 'lib/kknife/command.rb', line 128 def has_sub_command?( command ) @sub_commands.has_key? command end |
#pp ⇒ Object
Print current command as a tree, recurse through sub commands
100 101 102 103 104 105 |
# File 'lib/kknife/command.rb', line 100 def pp printf "%s%s\n", ' ' * level, cmd @sub_commands.each_key do |key| @sub_commands[key].pp end end |
#pp_single ⇒ Object
Print the current command on a single line if it’s the last in the tree, otherwise move down.
109 110 111 112 113 114 |
# File 'lib/kknife/command.rb', line 109 def pp_single printf "%s\n", command_lookup.reverse.join(' ') if has_no_sub_commands? @sub_commands.each_key do |key| @sub_commands[key].pp_single end end |
#process_lookup(commands) ⇒ Object
Recursively lookup a list of commands
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/kknife/command.rb', line 158 def process_lookup( commands ) raise NoMoreSuppliedCommands, "end of args" if commands.empty? commands_local = commands.dup first_command = commands_local.shift sub_command_list = sub_command_find first_command rest_commands = commands_local dbg 'first', first_command dbg 'list', sub_command_list dbg 'rest', rest_commands # If we have a command or can look one up, all is good if has_sub_command? first_command or sub_command_list.length == 1 cmd = sub_command first_command cmd ||= sub_command sub_command_list.first dbg 'cmd', cmd.cmd @controller.found_cmd cmd.cmd raise NoMoreSubCommands, "end of lookup commands" if cmd.sub_commands.empty? return cmd.process_lookup rest_commands # If there was more than one match, check the ambiguity config # or error elsif sub_command_list.length > 1 if Lookup.ambiguity( first_command ) process_lookup Lookup.ambiguity( first_command ) + rest_commands else raise AmbiguousCommand, "ambiguous [#{first_command}] [#{sub_command_list.join(',')}]" end # If there's an underscore, split it out elsif first_command.index(/_/) cmdsplit = first_command.split( /_/ ) # notify the controller of the command changes @controller.cmd_split cmdsplit # rebuild the local commands first_command = cmdsplit.shift rest_commands = cmdsplit + rest_commands # now start again with the new values process_lookup [ first_command ] + rest_commands # Otherwise the command wasn't found, # Look up shortcuts before giving up else shortcut = Lookup.shortcut( first_command ) if shortcut @controller.found_shortcut shortcut process_lookup shortcut + rest_commands else raise NotFoundCommand, "sub command not found: [#{first_command}*]" end end end |
#sub_command(command) ⇒ Object
Return a sub Comamnd by name
138 139 140 |
# File 'lib/kknife/command.rb', line 138 def sub_command( command ) @sub_commands[command] if has_sub_command? command end |
#sub_command_find(command) ⇒ Object
Find a sub Comamnd via substr
133 134 135 |
# File 'lib/kknife/command.rb', line 133 def sub_command_find( command ) @sub_commands.keys.grep( Regexp.new( '^' + Regexp.escape(command) + '.*' ) ) end |
#to_s ⇒ Object
return the command string
84 85 86 |
# File 'lib/kknife/command.rb', line 84 def to_s @cmd end |