Class: Cmd
- Inherits:
-
Object
- Object
- Cmd
- Extended by:
- ClassMethods
- Includes:
- ClassMethods
- Defined in:
- lib/cmd.rb
Overview
A simple framework for writing line-oriented command interpreters, based heavily on Python’s cmd.py.
These are often useful for test harnesses, administrative tools, and prototypes that will later be wrapped in a more sophisticated interface.
A Cmd instance or subclass instance is a line-oriented interpreter framework. There is no good reason to instantiate Cmd itself; rather, it’s useful as a superclass of an interpreter class you define yourself in order to inherit Cmd’s methods and encapsulate action methods.
Direct Known Subclasses
Defined Under Namespace
Modules: ClassMethods
Class Attribute Summary collapse
-
.hide_undocumented_commands ⇒ Object
Flag that sets whether undocumented commands are listed in the help.
Instance Attribute Summary collapse
-
#current_command ⇒ Object
writeonly
The current command.
-
#stdin ⇒ Object
writeonly
STDIN stream used.
-
#stdout ⇒ Object
writeonly
STDOUT stream used.
Class Method Summary collapse
Instance Method Summary collapse
-
#cmdloop(intro = nil) ⇒ Object
(also: #run)
Starts up the command loop.
- #do_exit ⇒ Object
- #do_help(command = nil) ⇒ Object
-
#initialize ⇒ Cmd
constructor
A new instance of Cmd.
-
#no_help(command) ⇒ Object
Called when the
command
has no associated documentation, this could potentially mean that the command is non existant. -
#turn_off_readline ⇒ Object
Turns off readline even if it is supported.
Methods included from ClassMethods
custom_exception_handlers, define_collect_method, doc, docs, handle, prompt, prompt_with, shortcut, shortcut_table, shortcuts
Constructor Details
#initialize ⇒ Cmd
Returns a new instance of Cmd.
147 148 149 150 151 |
# File 'lib/cmd.rb', line 147 def initialize @stdin, @stdout = STDIN, STDOUT @stop = false setup end |
Class Attribute Details
.hide_undocumented_commands ⇒ Object
Flag that sets whether undocumented commands are listed in the help
129 130 131 |
# File 'lib/cmd.rb', line 129 def hide_undocumented_commands @hide_undocumented_commands end |
Instance Attribute Details
#current_command=(value) ⇒ Object
The current command
143 144 145 |
# File 'lib/cmd.rb', line 143 def current_command=(value) @current_command = value end |
#stdin=(value) ⇒ Object (writeonly)
STDIN stream used
137 138 139 |
# File 'lib/cmd.rb', line 137 def stdin=(value) @stdin = value end |
#stdout=(value) ⇒ Object (writeonly)
STDOUT stream used
140 141 142 |
# File 'lib/cmd.rb', line 140 def stdout=(value) @stdout = value end |
Class Method Details
.run(intro = nil) ⇒ Object
131 132 133 |
# File 'lib/cmd.rb', line 131 def run(intro = nil) new.cmdloop(intro) end |
Instance Method Details
#cmdloop(intro = nil) ⇒ Object Also known as: run
Starts up the command loop
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cmd.rb', line 154 def cmdloop(intro = nil) preloop write intro if intro begin set_completion_proc(:complete) begin execute_command # Catch ^C rescue Interrupt user_interrupt # I don't know why ZeroDivisionError isn't caught below... rescue ZeroDivisionError handle_all_remaining_exceptions(ZeroDivisionError) rescue => exception handle_all_remaining_exceptions(exception) end end until @stop postloop end |
#do_exit ⇒ Object
194 |
# File 'lib/cmd.rb', line 194 def do_exit; stoploop end |
#do_help(command = nil) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/cmd.rb', line 177 def do_help(command = nil) if command command = translate_shortcut(command) docs.include?(command) ? print_help(command) : no_help(command) else documented_commands.each {|cmd| print_help cmd} print_undocumented_commands if undocumented_commands? end end |
#no_help(command) ⇒ Object
Called when the command
has no associated documentation, this could potentially mean that the command is non existant
189 190 191 |
# File 'lib/cmd.rb', line 189 def no_help(command) write "No help for command '#{command}'" end |
#turn_off_readline ⇒ Object
Turns off readline even if it is supported
197 198 199 200 |
# File 'lib/cmd.rb', line 197 def turn_off_readline @readline_supported = false self end |