Module: Executable

Included in:
Command
Defined in:
lib/executable.rb,
lib/executable/help.rb,
lib/executable/utils.rb,
lib/executable/domain.rb,
lib/executable/errors.rb,
lib/executable/parser.rb,
lib/executable/dispatch.rb

Overview

Executable is a mixin for creating robust, inheritable and reusable command line interfaces.

Defined Under Namespace

Modules: Dispatch, Domain, Utils Classes: Command, Help, NoCommandError, NoOptionError, Parser

Constant Summary collapse

Legacy =

Dispatch is Legacy.

Dispatch

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

When Exectuable is included into a class, the class is also extended by ‘Executable::Doamin`.



19
20
21
# File 'lib/executable.rb', line 19

def self.included(base)
  base.extend Domain
end

Instance Method Details

#call(*args) ⇒ Object

Command invocation abstract method.

Raises:

  • (NotImplementedError)


39
40
41
42
# File 'lib/executable.rb', line 39

def call(*args)
  #puts cli.show_help           # TODO: show help instead of error ?
  raise NotImplementedError
end

#cliObject

Access to underlying Help instance.



63
64
65
# File 'lib/executable.rb', line 63

def cli
  self.class.cli
end

#initialize(settings = {}) ⇒ Object

Default initializer, simply takes a hash of settings to set attributes via writer methods. Not existant attributes are simply ignored.



28
29
30
31
32
# File 'lib/executable.rb', line 28

def initialize(settings={})
  settings.each do |k,v|
    __send__("#{k}=", v) if respond_to?("#{k}=")
  end
end

#inspectObject



51
# File 'lib/executable.rb', line 51

alias_method :inspect, :to_s

#option_missing(opt, argv) ⇒ Object

Override option_missing if needed. This receives the name of the option and the remaining arguments list. It must consume any arguments it uses from the begining of the list (i.e. in-place manipulation).

Raises:



72
73
74
# File 'lib/executable.rb', line 72

def option_missing(opt, argv)
  raise NoOptionError, opt
end

#to_procObject

Convert Executable to Proc object.



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

def to_proc
  lambda { |*args| call(*args) }
end

#to_sObject

Output command line help.



56
57
58
# File 'lib/executable.rb', line 56

def to_s
  self.class.help.to_s  # usage ?
end