Class: Shellissimo::Shell

Inherits:
Object
  • Object
show all
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"

See Also:

Instance Method Summary collapse

Methods included from DSL

included

Constructor Details

#initializeShell

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

#runObject

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

Parameters:

  • str (String)

    command and parameters (like for REPL)



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