Class: Brut::CLI::Commands::Help

Inherits:
BaseCommand show all
Defined in:
lib/brut/cli/commands/help.rb

Defined Under Namespace

Classes: DescriptionList

Instance Attribute Summary collapse

Attributes inherited from BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#accepts, #args_description, #argv, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme

Constructor Details

#initialize(command, option_parser) ⇒ Help

Returns a new instance of Help.



5
6
7
8
# File 'lib/brut/cli/commands/help.rb', line 5

def initialize(command,option_parser)
  @command = command
  @option_parser = option_parser
end

Instance Attribute Details

#option_parserObject

Returns the value of attribute option_parser.



3
4
5
# File 'lib/brut/cli/commands/help.rb', line 3

def option_parser
  @option_parser
end

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


130
# File 'lib/brut/cli/commands/help.rb', line 130

def bootstrap? = false

#commandsObject



10
# File 'lib/brut/cli/commands/help.rb', line 10

def commands = []

#default_rack_envObject



131
# File 'lib/brut/cli/commands/help.rb', line 131

def default_rack_env = nil

#descriptionObject



2
# File 'lib/brut/cli/commands/help.rb', line 2

def description = "Get help for the app or a command"

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/brut/cli/commands/help.rb', line 46

def run
  cli = [@command.name ]
  cmd = @command
  while cmd.parent_command
    cmd = cmd.parent_command
    cli.unshift cmd.name
  end
  invocation = cli.join(" ")
  puts theme.code.render(invocation) + " - " + theme.title.render(theme.wrap(@command.description, indent: invocation.length + 3, max_width: 65).strip)
  puts
  usage = theme.code.render(invocation)
  options = @option_parser.top.list
  if options.size > 0
    usage << theme.weak.render(" [options]")
  end
  if @command.commands.any?
    usage << theme.code.render(" command")
  end
  if @command.args_description
    usage << " #{@command.args_description}"
  end
  puts theme.header.render("USAGE")
  puts
  puts "  " + usage
  puts
  if @command.detailed_description
    puts theme.header.render("DESCRIPTION")
    puts
    puts theme.wrap(@command.detailed_description.strip, indent: 2, max_width: 65)
    puts
  end
  if options.size > 0
    puts theme.header.render("OPTIONS")
    list = DescriptionList.new
    options.each do |option|
      switches = option.long.map { |switch|
        if option.arg
          if option.arg[0] == "="
            "#{switch.strip}#{theme.weak.render(option.arg.strip)}"
          else
            "#{switch.strip}=#{theme.weak.render(option.arg.strip)}"
          end
        else
          switch
        end
      } + option.short.map { |switch|
        if option.arg
          "#{switch} #{theme.weak.render(option.arg)}"
        else
          switch
        end
      }
      list << [
        switches.join("\n"),
        option.desc.join(" "),
      ]
    end
    puts list.lipgloss_table(theme:, terminal:).render
  end
  if @command.env_vars.any?
    puts theme.header.render("ENVIRONMENT VARIABLES")
    list = DescriptionList.new
    @command.env_vars.sort_by(&:first).each do |env_var|
      list << [
        env_var[0],
        env_var[1],
      ]
    end
    puts list.lipgloss_table(theme:, terminal:).render
  end
  if @command.commands.any?
    puts theme.header.render("COMMANDS")
    list = DescriptionList.new(term_align: :left, padding_bottom: 0)
    @command.commands.sort_by(&:name).each do |command|
      list << [
        command.name,
        command.description,
      ]
    end
    puts list.lipgloss_table(theme:, terminal:).render
  end
  0
end