Module: Cmd::ClassMethods

Included in:
Cmd, Cmd
Defined in:
lib/cmd.rb

Constant Summary collapse

@@docs =
{}
@@shortcuts =
{}
@@handlers =
{}
@@prompt =
'> '
@@shortcut_table =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_exception_handlersObject



106
107
108
# File 'lib/cmd.rb', line 106

def custom_exception_handlers
  @@handlers
end

.docsObject



38
39
40
# File 'lib/cmd.rb', line 38

def docs
  @@docs
end

.handle(exception, handler) ⇒ Object

Set what to do in the event that the given exception is raised.

handle StackOverflowError, :handle_stack_overflow


47
48
49
# File 'lib/cmd.rb', line 47

def handle(exception, handler)
  @@handlers[exception.to_s] = handler
end

.promptObject

Returns the evaluation of expression passed to prompt_with. Result has to_s called on it as Readline expects a String for its prompt. XXX This could probably be more robust



73
74
75
76
77
78
79
80
81
82
# File 'lib/cmd.rb', line 73

def prompt
  case @@prompt
  when Symbol 
      self.send @@prompt
  when Proc   
      @@prompt.call
  else         
      @@prompt
  end.to_s
end

.shortcut_tableObject



96
97
98
# File 'lib/cmd.rb', line 96

def shortcut_table
  @@shortcut_table
end

.shortcutsObject



101
102
103
# File 'lib/cmd.rb', line 101

def shortcuts
  @@shortcuts
end

Instance Method Details

#define_collect_method(prefix) ⇒ Object

Defines a method which returns all defined methods which start with the passed in prefix followed by an underscore. Used to define methods to collect things such as all defined ‘complete’ and ‘do’ methods.



114
115
116
117
118
119
120
121
# File 'lib/cmd.rb', line 114

def define_collect_method(prefix)
  method = 'collect_' + prefix
  unless self.respond_to?(method) 
    define_method(method) do
      self.methods.grep(/^#{prefix}_/).map {|meth| meth[prefix.size + 1..-1]}
    end
  end
end

#doc(command, docstring = nil) ⇒ Object

Set documentation for a command

doc :help, 'Display this help.'
def do_help
  # etc
end


33
34
35
36
# File 'lib/cmd.rb', line 33

def doc(command, docstring = nil)
  docstring = docstring ? docstring : yield
  @@docs[command.to_s] = docstring
end

#prompt_with(*p, &block) ⇒ Object

Sets what the prompt is. Accepts a String, a block or a Symbol.

Block

prompt_with { Time.now }

Symbol

prompt_with :set_prompt

String

prompt_with "#{self.class.name}> "


66
67
68
# File 'lib/cmd.rb', line 66

def prompt_with(*p, &block)
  @@prompt = block_given? ? block : p.first
end

#shortcut(short, command) ⇒ Object

Create a command short cut

shortcut '?', 'help'
def do_help
  # etc
end


91
92
93
94
# File 'lib/cmd.rb', line 91

def shortcut(short, command)
  (@@shortcuts[command.to_s] ||= []).push short
  @@shortcut_table[short] = command.to_s
end