Class: Tabry::UsageGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/tabry/usage_generator.rb

Constant Summary collapse

USAGE =
"Usage:".bold

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sub_stack, cmd_name) ⇒ UsageGenerator

Returns a new instance of UsageGenerator.



9
10
11
12
13
14
15
# File 'lib/tabry/usage_generator.rb', line 9

def initialize(sub_stack, cmd_name)
  @sub_stack = sub_stack
  @sub = sub_stack.last
  non_main_subs = sub_stack[1..]
  @cmdline_string = [cmd_name, *non_main_subs.map(&:name)].join(" ")
  @top_level = sub_stack.length == 1
end

Instance Attribute Details

#cmdline_stringObject (readonly)

Returns the value of attribute cmdline_string.



7
8
9
# File 'lib/tabry/usage_generator.rb', line 7

def cmdline_string
  @cmdline_string
end

#subObject (readonly)

Returns the value of attribute sub.



7
8
9
# File 'lib/tabry/usage_generator.rb', line 7

def sub
  @sub
end

#sub_stackObject (readonly)

Returns the value of attribute sub_stack.



7
8
9
# File 'lib/tabry/usage_generator.rb', line 7

def sub_stack
  @sub_stack
end

#top_levelObject (readonly)

Returns the value of attribute top_level.



7
8
9
# File 'lib/tabry/usage_generator.rb', line 7

def top_level
  @top_level
end

Instance Method Details

#add_text_with_indent(lines, text, indent_string) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/tabry/usage_generator.rb', line 51

def add_text_with_indent(lines, text, indent_string)
  return unless text

  text.split("\n").each do |line|
    lines << (indent_string + line)
  end
end

#usageObject



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
# File 'lib/tabry/usage_generator.rb', line 19

def usage
  lines = []
  if description
    lines << description
    lines << ""
  end
  if subs.any?
    lines << "#{USAGE} #{cmdline_string} <subcommand> ..."
  end

  if args.any?
    arg_strings = args.map do |a|
      name = a.title || "arg"
      str = "<#{name}>"
      str += " [<#{name}>...]" if a.varargs?
      str = "[#{str}]" if a.optional
      str
    end
    lines << ["#{USAGE} #{cmdline_string}", *arg_strings].join(" ")
  end

  if subs.empty? && args.empty?
    lines << "#{USAGE} #{cmdline_string}"
  end

  usage_add_subcommands(lines, top_level)
  usage_add_args(lines)
  usage_add_flags(lines)

  lines.join("\n")
end

#usage_add_args(lines) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tabry/usage_generator.rb', line 118

def usage_add_args(lines)
  if args.any?(&:description)
    lines << ""
    lines << "ARGS".green.bold
    args.each do |arg|
      arg_name = arg.name.bold
      arg_name += " (optional)" if arg.optional
      lines << arg_name
      add_text_with_indent(lines, arg.description, "  ")
    end
  end
end

#usage_add_flags(lines) ⇒ Object



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
# File 'lib/tabry/usage_generator.rb', line 87

def usage_add_flags(lines)
  partial_sub_stack = []
  sub_stack_with_names = sub_stack.map do |sub|
    partial_sub_stack << sub.name
    [sub, partial_sub_stack.compact.join(" ")]
  end

  sub_stack_with_names.reverse.each do |sub, full_sub_name|
    next unless sub.flags.any?

    lines << ""
    lines << "FLAGS".green.bold
    if sub != sub_stack.last
      lines[-1] += if full_sub_name == ""
                     " (global)".green.bold
                   else
                     " (#{full_sub_name})".green.bold
                   end
    end

    sub.flags.map(&:name).sort.each do |name|
      flag = sub.flags[name]
      flag_name = [flag.name, *flag.aliases].map { |al| flag.alias_with_dash(al).bold }.join(", ")
      flag_name << " <arg>" if flag.arg
      flag_name << " (required)" if flag.required
      lines << flag_name
      add_text_with_indent(lines, flag.description, "  ")
    end
  end
end

#usage_add_subcommands(lines, top_level) ⇒ Object



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
# File 'lib/tabry/usage_generator.rb', line 59

def usage_add_subcommands(lines, top_level)
  if subs.any? || top_level
    lines << ""
    lines << "SUBCOMMANDS".green.bold
  end

  subs_by_name = subs.by_name
  sub_names = subs_by_name.keys
  sub_names |= ["help"] if top_level

  sub_names.sort.each do |name|
    if (s = subs_by_name[name])
      name_line = name.bold
      if s.aliases&.length == 1
        name_line += " (alias: #{s.aliases.first})"
      elsif s.aliases
        name_line += " (aliases: #{s.aliases.join(", ")})"
      end

      lines << name_line
      add_text_with_indent(lines, s.description, "  ")
    elsif name == "help"
      lines << "help".bold
      lines << "  Show help on command or subcommand"
    end
  end
end