Class: GLI::Commands::HelpModules::GlobalHelpFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/leap_cli/lib_ext/gli.rb

Constant Summary collapse

SUB_CMD_INDENT =
"  "

Instance Method Summary collapse

Instance Method Details

#formatObject



10
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/leap_cli/lib_ext/gli.rb', line 10

def format
  program_desc = @app.program_desc
  program_long_desc = @app.program_long_desc
  if program_long_desc
    wrapper = @wrapper_class.new(Terminal.instance.size[0],4)
    program_long_desc = "\n    #{wrapper.wrap(program_long_desc)}\n\n" if program_long_desc
  else
    program_long_desc = "\n"
  end

  # build a list of commands, sort them so the commands with subcommands are at the bottom
  commands = @sorter.call(@app.commands_declaration_order.reject(&:nodoc)).sort do |a,b|
    if a.commands.any? && b.commands.any?;  a.name.to_s <=> b.name.to_s
    elsif a.commands.any?;                  1
    elsif b.commands.any?;                 -1
    else;                                   a.name.to_s <=> b.name.to_s
    end
  end

  # build a list of command info ([name, description]), including subcommands if appropriate
  command_info_list = []
  commands.each do |command|
    name = [command.name, Array(command.aliases)].flatten.join(', ')
    command_info_list << [name, command.description]
    if command.commands.any?
      @sorter.call(command.commands_declaration_order).each do |cmd|
        command_info_list << [SUB_CMD_INDENT + command.name.to_s + " " + cmd.names, cmd.description + (command.get_default_command == cmd.name ? " (default)" : "")]
      end
    end
  end

  # display
  command_formatter = ListFormatter.new(command_info_list, @wrapper_class)
  stringio = StringIO.new
  command_formatter.output(stringio)
  commands = stringio.string
  global_option_descriptions = OptionsFormatter.new(global_flags_and_switches, @sorter, @wrapper_class).format
  GLOBAL_HELP.result(binding)
end