Module: Ace::Support::Cli::Help::HelpCommand

Defined in:
lib/ace/support/cli/help/help_command.rb

Class Method Summary collapse

Class Method Details

.build(program_name:, version:, commands:, examples: nil) ⇒ Object



8
9
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ace/support/cli/help/help_command.rb', line 8

def self.build(program_name:, version:, commands:, examples: nil)
  Class.new(Command) do
    @program_name = program_name
    @version = version
    @commands = commands
    @examples = examples

    class << self
      attr_reader :program_name, :version, :commands, :examples
    end

    desc "Show top-level help"
    argument :args, type: :array, required: false

    def call(**_params)
      puts self.class.render
      0
    end

    def self.render
      sections = []
      sections << "#{program_name} #{version}".strip
      sections << render_commands
      rendered_examples = render_examples
      sections << rendered_examples if rendered_examples
      sections << render_options
      sections.join("\n\n")
    end

    def self.render_commands
      lines = normalized_commands.map do |name, description|
        "#{"  #{name}".ljust(16)}# #{description}"
      end
      "Commands:\n#{lines.join("\n")}"
    end

    def self.render_examples
      return nil if examples.nil? || examples.empty?

      "Examples:\n#{examples.map { |item| "  #{item}" }.join("\n")}"
    end

    def self.render_options
      <<~OPTIONS.chomp
        Options:
          --help, -h      # Print this help
          --version       # Print version
      OPTIONS
    end

    def self.normalized_commands
      commands.is_a?(Hash) ? commands.to_a : commands
    end
  end
end