Class: Slk::Support::HelpFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/support/help_formatter.rb

Overview

Formats help text with auto-aligned columns

Example usage:

help = HelpFormatter.new("slk status [text] [emoji] [options]")
help.description("Get or set your Slack status.")
help.note("GET shows all workspaces by default. SET applies to primary only.")

help.section("EXAMPLES") do |s|
  s.example("slk status", "Show status (all workspaces)")
  s.example("slk status clear", "Clear status")
end

help.section("OPTIONS") do |s|
  s.option("-n, --limit N", "Messages per channel (default: 10)")
  s.option("--muted", "Include muted channels")
end

puts help.render

Defined Under Namespace

Classes: Section

Instance Method Summary collapse

Constructor Details

#initialize(usage) ⇒ HelpFormatter

Returns a new instance of HelpFormatter.



25
26
27
28
29
30
# File 'lib/slk/support/help_formatter.rb', line 25

def initialize(usage)
  @usage = usage
  @description = nil
  @notes = []
  @sections = []
end

Instance Method Details

#description(text) ⇒ Object



32
33
34
35
# File 'lib/slk/support/help_formatter.rb', line 32

def description(text)
  @description = text
  self
end

#note(text) ⇒ Object



37
38
39
40
# File 'lib/slk/support/help_formatter.rb', line 37

def note(text)
  @notes << text
  self
end

#renderObject



49
50
51
52
53
54
# File 'lib/slk/support/help_formatter.rb', line 49

def render
  lines = build_header
  lines.concat(build_sections)
  lines.pop if lines.last == ''
  lines.join("\n")
end

#section(title, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/slk/support/help_formatter.rb', line 42

def section(title, &block)
  section = Section.new(title)
  block.call(section)
  @sections << section
  self
end