Class: Clive::Formatter Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/clive/formatter.rb,
lib/clive/formatter/plain.rb,
lib/clive/formatter/colour.rb

Overview

This class is abstract.

Subclass and override #to_s (and probably #initialize) to implement a custom Formatter. #initialize should take an options hash.

Takes care of formatting the help string. Look at Plain for a good (if not a bit complex) reference of how to do it.

Then it is just a case of passing an instance of the new formatter to run. You can use a different formatter for commands by passing it when creating them.

Examples:


class MainFormatter < Clive::Formatter
  # ...
end

class CommandFormatter < Clive::Formatter
  # ...
end

# Uses MainFormatter
class CLI
  # ...

  # Uses CommandFormatter
  command :new, formatter: CommandFormatter.new do
    # ...
  end

  # Uses MainFormatter
  command :normal do
    # ...
  end
end

CLI.run formatter: MainFormatter.new

Direct Known Subclasses

Plain

Defined Under Namespace

Classes: Colour, Plain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Formatter

Returns a new instance of Formatter.


45
46
47
48
49
50
# File 'lib/clive/formatter.rb', line 45

def initialize(opts={})
  @opts = opts

  @header, @footer = '', ''
  @commands, @options = [], []
end

Instance Attribute Details

#commands=(value) ⇒ Object (writeonly)

Sets the attribute commands

Parameters:

  • value

    the value to set the attribute commands to.


43
44
45
# File 'lib/clive/formatter.rb', line 43

def commands=(value)
  @commands = value
end

#footer=(value) ⇒ Object (writeonly)

Sets the attribute footer

Parameters:

  • value

    the value to set the attribute footer to.


43
44
45
# File 'lib/clive/formatter.rb', line 43

def footer=(value)
  @footer = value
end

#header=(value) ⇒ Object (writeonly)

Sets the attribute header

Parameters:

  • value

    the value to set the attribute header to.


43
44
45
# File 'lib/clive/formatter.rb', line 43

def header=(value)
  @header = value
end

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.


43
44
45
# File 'lib/clive/formatter.rb', line 43

def options=(value)
  @options = value
end

Instance Method Details

#inspectObject


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

def inspect
  "#<#{self.class.name} @opts=#@opts>"
end

#to_sObject


52
53
54
# File 'lib/clive/formatter.rb', line 52

def to_s
  ([@header] + @commands + @options + [@footer]).join("\n")
end