Class: CmdParse::Command
- Inherits:
-
Object
- Object
- CmdParse::Command
- Defined in:
- lib/module_cmdparse/cmdparse.rb
Overview
Base class for the commands. This class implements all needed methods so that it can be used by the CommandParser
class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the list of commands for this command.
-
#default_command ⇒ Object
readonly
Returns the name of the default command.
-
#description ⇒ Object
A detailed description of the command.
-
#name ⇒ Object
readonly
The name of the command.
-
#options ⇒ Object
The wrapper for parsing the command line options.
-
#pen ⇒ Object
used for printing.
-
#short_desc ⇒ Object
A short description of the command.
-
#super_command ⇒ Object
Sets or returns the super command of this command.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
For sorting commands by name.
-
#add_command(command, default = false) ⇒ Object
Adds a command to the command list if this command takes other commands as argument.
-
#commandparser ⇒ Object
Returns the
CommandParser
instance for this command ornil
if this command was not assigned to aCommandParser
instance. -
#execute(args) ⇒ Object
Invokes the block set by
set_execution_block
. -
#has_commands? ⇒ Boolean
Returns
true
if this command supports sub commands. -
#init ⇒ Object
This method is called when the command is added to a
Command
instance. -
#initialize(name, has_commands, partial_commands = false) ⇒ Command
constructor
Initializes the command called
name
. -
#set_execution_block(&block) ⇒ Object
Set the given
block
as execution block. -
#show_help ⇒ Object
Default method for showing the help for the command.
-
#super_commands ⇒ Object
Returns a list of super commands, ie.: [command, super_command, super_super_command, …].
-
#usage ⇒ Object
Defines the usage line for the command.
- #use_partial_commands(use_partial) ⇒ Object
Constructor Details
#initialize(name, has_commands, partial_commands = false) ⇒ Command
Initializes the command called name
. The parameter has_commands
specifies if this command takes other commands as argument. The optional argument partial_commands
specifies, if partial command matching should be used.
156 157 158 159 160 161 162 163 164 |
# File 'lib/module_cmdparse/cmdparse.rb', line 156 def initialize( name, has_commands, partial_commands = false ) @name = name @options = ParserWrapper.new @has_commands = has_commands @commands = Hash.new @default_command = nil @pen = nil use_partial_commands( partial_commands ) end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the list of commands for this command.
148 149 150 |
# File 'lib/module_cmdparse/cmdparse.rb', line 148 def commands @commands end |
#default_command ⇒ Object (readonly)
Returns the name of the default command.
141 142 143 |
# File 'lib/module_cmdparse/cmdparse.rb', line 141 def default_command @default_command end |
#description ⇒ Object
A detailed description of the command
135 136 137 |
# File 'lib/module_cmdparse/cmdparse.rb', line 135 def description @description end |
#name ⇒ Object (readonly)
The name of the command
129 130 131 |
# File 'lib/module_cmdparse/cmdparse.rb', line 129 def name @name end |
#options ⇒ Object
The wrapper for parsing the command line options.
138 139 140 |
# File 'lib/module_cmdparse/cmdparse.rb', line 138 def @options end |
#pen ⇒ Object
used for printing
151 152 153 |
# File 'lib/module_cmdparse/cmdparse.rb', line 151 def pen @pen end |
#short_desc ⇒ Object
A short description of the command.
132 133 134 |
# File 'lib/module_cmdparse/cmdparse.rb', line 132 def short_desc @short_desc end |
#super_command ⇒ Object
Sets or returns the super command of this command. The super command is either a Command
instance for normal commands or a CommandParser
instance for the root command.
145 146 147 |
# File 'lib/module_cmdparse/cmdparse.rb', line 145 def super_command @super_command end |
Instance Method Details
#<=>(other) ⇒ Object
For sorting commands by name.
189 190 191 |
# File 'lib/module_cmdparse/cmdparse.rb', line 189 def <=>( other ) @name <=> other.name end |
#add_command(command, default = false) ⇒ Object
Adds a command to the command list if this command takes other commands as argument. If the optional parameter default
is true, then this command is used when no command is specified on the command line.
180 181 182 183 184 185 186 |
# File 'lib/module_cmdparse/cmdparse.rb', line 180 def add_command( command, default = false ) raise TakesNoCommandError.new( @name ) if !has_commands? @commands[command.name] = command @default_command = command.name if default command.super_command = self command.init end |
#commandparser ⇒ Object
Returns the CommandParser
instance for this command or nil
if this command was not assigned to a CommandParser
instance.
195 196 197 198 199 |
# File 'lib/module_cmdparse/cmdparse.rb', line 195 def commandparser cmd = super_command cmd = cmd.super_command while !cmd.nil? && !cmd.kind_of?( CommandParser ) cmd end |
#execute(args) ⇒ Object
Invokes the block set by set_execution_block
. This method is called by the CommandParser
instance if this command was specified on the command line.
222 223 224 |
# File 'lib/module_cmdparse/cmdparse.rb', line 222 def execute( args ) @exec_block.call( args ) end |
#has_commands? ⇒ Boolean
Returns true
if this command supports sub commands.
173 174 175 |
# File 'lib/module_cmdparse/cmdparse.rb', line 173 def has_commands? @has_commands end |
#init ⇒ Object
This method is called when the command is added to a Command
instance.
213 |
# File 'lib/module_cmdparse/cmdparse.rb', line 213 def init; end |
#set_execution_block(&block) ⇒ Object
Set the given block
as execution block. See also: execute
.
216 217 218 |
# File 'lib/module_cmdparse/cmdparse.rb', line 216 def set_execution_block( &block ) @exec_block = block end |
#show_help ⇒ Object
Default method for showing the help for the command.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/module_cmdparse/cmdparse.rb', line 239 def show_help @pen.puts "#{@pen.lightgreen}#{@name}#{@pen.clear} - #{short_desc}" @pen.puts description.split("\n").each { |line| @pen.puts line } if description @pen.puts @pen.puts usage @pen.puts if has_commands? list_commands @pen.puts end unless (summary = .summarize).empty? summary.each do |line| @pen.puts line.gsub(/(-\w,)/, @pen.lightyellow+'\1'+@pen.clear).gsub(/(--[\w]*)/,@pen.lightyellow+'\1'+@pen.clear).gsub(/(<.*>)/,@pen.lightyellow+'\1'+@pen.clear) end @pen.puts end end |
#super_commands ⇒ Object
Returns a list of super commands, ie.:
[command, super_command, super_super_command, ...]
202 203 204 205 206 207 208 209 210 |
# File 'lib/module_cmdparse/cmdparse.rb', line 202 def super_commands cmds = [] cmd = self while !cmd.nil? && !cmd.super_command.kind_of?( CommandParser ) cmds << cmd cmd = cmd.super_command end cmds end |
#usage ⇒ Object
Defines the usage line for the command.
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/module_cmdparse/cmdparse.rb', line 227 def usage tmp = "#{@pen.bold}Usage#{@pen.clear}:#{@pen.lightcyan} #{commandparser.program_name}#{@pen.clear}" tmp << "#{@pen.lightyellow} <global-options> #{@pen.clear}" if !commandparser..instance_of?( ParserWrapper ) tmp << super_commands.reverse.collect do |c| t = @pen.lightcyan+c.name+@pen.clear t << "#{@pen.lightyellow} <local-options>#{@pen.clear}" if !c..instance_of?( ParserWrapper ) t end.join(' ') tmp << (has_commands? ? "#{@pen.lightcyan} COMMAND #{@pen.lightyellow}<options>#{@pen.lightgreen} [ARGS]#{@pen.clear}" : "#{@pen.lightgreen} [ARGS]#{@pen.clear}") end |
#use_partial_commands(use_partial) ⇒ Object
166 167 168 169 170 |
# File 'lib/module_cmdparse/cmdparse.rb', line 166 def use_partial_commands( use_partial ) temp = ( use_partial ? CommandHash.new : Hash.new ) temp.update( @commands ) @commands = temp end |