Module: Mothership::Help

Defined in:
lib/mothership/help.rb,
lib/mothership/help/printer.rb

Constant Summary collapse

@@groups =
[]
@@tree =
{}

Class Method Summary collapse

Class Method Details

.add_to_group(command, names, options) ⇒ Object



28
29
30
31
32
33
# File 'lib/mothership/help.rb', line 28

def add_to_group(command, names, options)
  where = find_group(names, @@tree)
  raise "unknown help group: #{names.join("/")}" unless where
  
  where[:members] << [command, options]
end

.basic_help(commands, global) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mothership/help/printer.rb', line 50

def basic_help(commands, global)
  puts "Commands:"

  width = 0
  commands.each do |_, c|
    len = c.usage.size
    width = len if len > width
  end

  commands.each do |_, c|
    puts "  #{c.usage.ljust(width)}\t#{c.description}"
  end

  unless global.flags.empty?
    puts ""
    command_options(global)
  end
end

.command_help(cmd) ⇒ Object



69
70
71
72
73
# File 'lib/mothership/help/printer.rb', line 69

def command_help(cmd)
  puts cmd.description
  puts ""
  command_usage(cmd)
end

.command_options(cmd, io = $stdout) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mothership/help/printer.rb', line 84

def command_options(cmd, io = $stdout)
  io.puts "Options:"

  rev_flags = Hash.new { |h, k| h[k] = [] }

  cmd.flags.each do |f, n|
    rev_flags[n] << f
  end

  usages = []

  max_width = 0
  rev_flags.collect do |name, fs|
    info = cmd.inputs[name]
    next if info[:hidden]

    flag = name.to_s.gsub("_", "-")

    # move full form to the front
    fs.unshift fs.delete("--#{flag}")

    if short = fs.find { |x| x =~ /^-.$/ }
      fs.delete short
    end

    if info[:type] == :boolean && info[:default]
      fs[0] = "--[no-]#{flag}"
    end

    if info.key?(:default) && info.key?(:interact)
      fs.unshift "--ask-#{flag}"
    end

    usage = "#{short ? short + "," : "   "} #{fs.join ", "}"

    unless info[:type] == :boolean
      usage << " #{(info[:value] || name).to_s.upcase}"
    end

    max_width = usage.size if usage.size > max_width

    usages << [usage, info[:description]]
  end

  usages.sort! { |a, b| a.first <=> b.first }

  usages.each do |u, d|
    if d
      io.puts "  #{u.ljust(max_width)}    #{d}"
    else
      io.puts "  #{u}"
    end
  end
end

.command_usage(cmd, io = $stdout) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/mothership/help/printer.rb', line 75

def command_usage(cmd, io = $stdout)
  io.puts "Usage: #{cmd.usage}"

  unless cmd.flags.empty?
    io.puts ""
    command_options(cmd, io)
  end
end

.group(*names) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/mothership/help.rb', line 20

def group(*names)
  if where = find_group(names, @@tree)
    where[:members].collect(&:first)
  else
    []
  end
end

.groups(*tree) ⇒ Object

define help groups



14
15
16
17
18
# File 'lib/mothership/help.rb', line 14

def groups(*tree)
  tree.each do |*args|
    add_group(@@groups, @@tree, *args.first)
  end
end

.has_groups?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mothership/help.rb', line 9

def has_groups?
  !@@groups.empty?
end


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
42
43
44
45
46
47
48
# File 'lib/mothership/help/printer.rb', line 11

def print_help_group(group, all = false, indent = 0)
  return if nothing_printable?(group, all)

  members = group[:members]

  unless all
    members = members.reject do |_, opts|
      opts[:hidden]
    end
  end

  commands = members.collect(&:first)

  i = "  " * indent

  print i
  puts group[:description]

  commands = unique_commands(commands)

  width = 0
  commands.each do |cmd|
    len = cmd.usage.size
    if len > width
      width = len
    end
  end

  commands.each do |cmd|
    puts "#{i}  #{cmd.usage.ljust(width)}\t#{cmd.description}"
  end

  puts "" unless commands.empty?

  group[:children].each do |group|
    print_help_group(group, all, indent + 1)
  end
end


3
4
5
6
7
8
9
# File 'lib/mothership/help/printer.rb', line 3

def print_help_groups(global = nil, all = false)
  @@groups.each do |commands|
    print_help_group(commands, all)
  end

  command_options(global)
end