class Argy::Help

Builds help information

Attributes

column[R]
parser[R]

Public Class Methods

new(parser, column: 30, color: $stdout.tty?) click to toggle source

Create a new Help @param parser [Parser] the parser to generate help for @param column [Integer] the column width of the help @param color [TrueClass,FalseClass] whether or not to print with color

# File lib/argy/help.rb, line 8
def initialize(parser, column: 30, color: $stdout.tty?)
  @parser = parser
  @column = column
  @color = color
end

Public Instance Methods

entry(name, desc: nil, required: false, default: nil) click to toggle source

Format an entry of a section @param name [String] left column of the entry @param desc [String] right column of the entry @param required [TrueClass,FalseClass] whether or not the entry is required @param default [Object] default value for the entry @return [String]

# File lib/argy/help.rb, line 41
def entry(name, desc: nil, required: false, default: nil)
  out = "  #{name.ljust(column)}"
  out += dim("#{desc} ") if desc
  out += dim("(required) ") if required
  out += dim("[default: #{default.inspect}]") if default
  out.rstrip
end
section(title) click to toggle source

Format the title of a custom section @return [String]

# File lib/argy/help.rb, line 31
def section(title)
  bold "\n#{title}"
end
to_s() click to toggle source

The help information @return [String]

# File lib/argy/help.rb, line 16
def to_s
  out = []

  description(out)
  usage(out)
  examples(out)
  arguments(out)
  options(out)
  flags(out)

  out.join("\n") + "\n"
end

Private Instance Methods

argument(a) click to toggle source
# File lib/argy/help.rb, line 70
def argument(a)
  entry(a.label, desc: a.desc, required: a.required?, default: a.default)
end
arguments(out) click to toggle source
# File lib/argy/help.rb, line 65
def arguments(out)
  out << bold("\nARGUMENTS") if parser.arguments.any?
  out.concat parser.arguments.map { |a| argument(a) }
end
bold(text) click to toggle source
# File lib/argy/help.rb, line 101
def bold(text)
  color? ? "\e[1m#{text}\e[0m" : text
end
color?() click to toggle source
# File lib/argy/help.rb, line 109
def color?
  @color
end
description(out) click to toggle source
# File lib/argy/help.rb, line 51
def description(out)
  out << "#{parser.description}\n" if parser.description
end
dim(text) click to toggle source
# File lib/argy/help.rb, line 105
def dim(text)
  color? ? "\e[2m#{text}\e[0m" : text
end
examples(out) click to toggle source
# File lib/argy/help.rb, line 60
def examples(out)
  out << bold("\nEXAMPLES") if parser.examples.any?
  out.concat parser.examples.map { |ex| "  #{ex}" }
end
flag(flag) click to toggle source
# File lib/argy/help.rb, line 90
def flag(flag)
  flag = flag.dup
  desc = flag.pop unless flag.last.match?(/^-/)
  entry(flag.reverse.join(", "), desc: desc)
end
flags(out) click to toggle source
# File lib/argy/help.rb, line 85
def flags(out)
  out << bold("\nFLAGS") if parser.flags.any?
  out.concat parser.flags.map { |f, _| flag(f) }
end
option(o) click to toggle source
# File lib/argy/help.rb, line 79
def option(o)
  label = [option_label(o.label, o.type)]
  label += o.aliases.map { |a| option_label(a, o.type) }
  entry(label.join(", "), desc: o.desc, required: o.required?, default: o.default)
end
option_label(label, type) click to toggle source
# File lib/argy/help.rb, line 96
def option_label(label, type)
  return label if type == :boolean
  label.start_with?("--") ? "#{label}=VALUE" : "#{label} VALUE"
end
options(out) click to toggle source
# File lib/argy/help.rb, line 74
def options(out)
  out << bold("\nOPTIONS") if parser.options.any?
  out.concat parser.options.map { |o| option(o) }
end
usage(out) click to toggle source
# File lib/argy/help.rb, line 55
def usage(out)
  out << bold("USAGE")
  out << "  #{parser.usage}"
end