Class: Hobo::HelpFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/hobo/help_formatter.rb

Constant Summary collapse

ALIGN_PAD =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global) ⇒ HelpFormatter

Returns a new instance of HelpFormatter.



6
7
8
9
# File 'lib/hobo/help_formatter.rb', line 6

def initialize global
  @global = global
  @command_map = {}
end

Instance Attribute Details

#command_mapObject

Returns the value of attribute command_map.



3
4
5
# File 'lib/hobo/help_formatter.rb', line 3

def command_map
  @command_map
end

Instance Method Details

#help(opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hobo/help_formatter.rb', line 11

def help opts = {}
  target = opts[:target] || nil
  output = [""]

  command = @command_map[target]

  if !command
    command_opts = []
    commands = commands_for(@global, opts)
    command = @global
  else
    command_opts = options_for(command)
    commands = commands_for(command, opts)
  end

  global_opts = options_for(@global)

  align_to = longest([global_opts, command_opts, commands]) + ALIGN_PAD

  if command != @global
    description = [ command.long_description, command.description ].compact.first
    output.push Hobo.ui.color("#{description}\n", :description) if description
  end

  output.push section("Usage", [usage(command, target)])
  output.push section("Global options", global_opts, align_to)
  output.push section("Command options", command_opts, align_to)
  output.push section("Commands", commands, align_to)

  return output.compact.join("\n")
end