Class: Shellissimo::Shell
- Inherits:
-
Object
- Object
- Shellissimo::Shell
- Includes:
- DSL
- Defined in:
- lib/shellissimo/shell.rb
Overview
Base class for Shells Allows to define commands via DSL Arovides ‘help’ and ‘quit’ commands out-of-box
Supports REPL and one-shot mode
Commands are instance-evaled against Shell instance, so all instance variables are available in command blocks.
Example:
class MyShell < Shellissimo::Shell
command :hi do |c|
c.description "Says hello to the user"
c.run { |params| @greeter.say_hi(params[:user]) }
end
def initialize
@greeter = Greeter.new
end
end
Usage:
-> hi user: "vlyrs"
Instance Method Summary collapse
-
#initialize ⇒ Shell
constructor
A new instance of Shell.
-
#run ⇒ Object
Runs REPL.
-
#run_command(str) ⇒ Object
Runs a single command.
Methods included from DSL
Constructor Details
#initialize ⇒ Shell
Returns a new instance of Shell.
52 53 54 |
# File 'lib/shellissimo/shell.rb', line 52 def initialize(*) @input_parser = InputParser.new(self.class.commands) end |
Instance Method Details
#run ⇒ Object
Note:
swallows exceptions!
Runs REPL
60 61 62 63 64 |
# File 'lib/shellissimo/shell.rb', line 60 def run while buf = Readline.readline(prompt, true) do run_command(buf) end end |
#run_command(str) ⇒ Object
Note:
swallows exceptions!
Runs a single command
71 72 73 74 75 76 |
# File 'lib/shellissimo/shell.rb', line 71 def run_command(str) Shellissimo.with_error_handling do command = @input_parser.parse_command(str) puts format instance_eval(&command.block) end end |