Class: Ame::Help::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/help/terminal.rb

Overview

Outputs help requests to a pair of IO objects, defaulting to ‘$stdout` and `$stderr`. An instance of this class is used by default for outputting help requests from Root, but can be overridden by invoking Root.help with another object (such as an instance of this class that’s been constructed with different parameters).

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, error = $stderr, exit_on_error = true) ⇒ Terminal

Sets up help requests to be made to OUTPUT (#dispatch, #method, and #version) and ERROR (#error), as well as specifying whether to EXIT_ON_ERROR or not, see #error.

Parameters:

  • output (#puts) (defaults to: $stdout)
  • error (#puts) (defaults to: $stderr)
  • exit_on_error (Boolean) (defaults to: true)


16
17
18
# File 'lib/ame-1.0/help/terminal.rb', line 16

def initialize(output = $stdout, error = $stderr, exit_on_error = true)
  @output, @error, @exit_on_error = output, error, exit_on_error
end

Instance Method Details

#dispatch(method, klass) ⇒ self

Outputs a help request for a Class.dispatch METHOD to KLASS, displaying all options and arguments to the method and listing the possible dispatch methods.

Parameters:

Returns:

  • (self)


26
27
28
29
30
31
32
# File 'lib/ame-1.0/help/terminal.rb', line 26

def dispatch(method, klass)
  output(method_s(method).tap{ |result|
    append_group result, 'Methods', klass.methods.sort_by{ |m| m.name } do |m|
      m.name
    end
  })
end

#error(method, error) ⇒ Object

Outputs ERROR that occurred while processing METHOD.

Raises:

  • (SystemExit)

    If exit_on_error was given as true to the receiver’s constructor

  • (error)

    If exit_on_error wasn’t given as true to the receiver’s constructor



55
56
57
58
59
# File 'lib/ame-1.0/help/terminal.rb', line 55

def error(method, error)
  errput '%s: %s' % [method, error]
  exit 1 if @exit_on_error
  raise error
end

#method(method) ⇒ self

Outputs a help request for METHOD, displaying all its options and arguments.

Parameters:

Returns:

  • (self)


38
39
40
# File 'lib/ame-1.0/help/terminal.rb', line 38

def method(method)
  output(method_s(method))
end

#version(method, version) ⇒ self

Outputs VERSION information for METHOD.

Parameters:

  • method (Method)
  • version (String)

Returns:

  • (self)


46
47
48
# File 'lib/ame-1.0/help/terminal.rb', line 46

def version(method, version)
  output('%s %s' % [method.name, version])
end