Class: Brut::CLI::Commands::HelpInMarkdown

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/brut/cli/commands/help_in_markdown.rb

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) ⇒ HelpInMarkdown

Returns a new instance of HelpInMarkdown.



5
6
7
8
# File 'lib/brut/cli/commands/help_in_markdown.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_in_markdown.rb', line 3

def option_parser
  @option_parser
end

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


107
# File 'lib/brut/cli/commands/help_in_markdown.rb', line 107

def bootstrap? = false

#commandsObject



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

def commands = []

#default_rack_envObject



108
# File 'lib/brut/cli/commands/help_in_markdown.rb', line 108

def default_rack_env = nil

#descriptionObject



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

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

#runObject



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
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
# File 'lib/brut/cli/commands/help_in_markdown.rb', line 12

def run
  if env["BRUT_HELP_IN_MARKDOWN_COMMANDS_ONLY"]
    @command.commands.sort_by(&:name).each do |command|
      puts command.name
    end
    return 0
  end
  cli = [@command.name ]
  cmd = @command
  while cmd.parent_command
    cmd = cmd.parent_command
    cli.unshift cmd.name
  end
  invocation = cli.join(" ")
  puts "# `#{invocation}`"
  puts
  puts @command.description
  puts

  usage = 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
  puts "## USAGE"
  puts
  puts "    " + usage
  puts
  if @command.detailed_description
    puts
    puts "## DESCRIPTION"
    puts
    puts @command.detailed_description.gsub(/ +/," ").strip
    puts
  end
  if options.size > 0
    puts
    puts "## OPTIONS"
    puts

    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
      }
      puts "* `#{switches.join(", ")}` - #{option.desc.join(" ")}"
    end
  end
  if @command.env_vars.any?
    puts
    puts "## ENVIRONMENT VARIABLES"
    puts
    @command.env_vars.sort_by(&:first).each do |env_var|
      puts "* `#{env_var[0]}` -  #{env_var[1]}"
    end
  end
  if @command.commands.any?
    commands_subpath = env["BRUT_HELP_IN_MARKDOWN_COMMAND_PATH"] || "commands"
    puts
    puts "## COMMANDS"
    puts
    @command.commands.sort_by(&:name).each do |command|
      puts "### [`#{command.name}`](./#{commands_subpath}/#{command.name})"
      puts
      puts "#{command.description}"
      if command.detailed_description
        puts
        puts command.detailed_description.gsub(/ +/," ").strip
      end
    end
  end
  0
end