Class: Cl::Help::Cmd

Inherits:
Struct
  • Object
show all
Includes:
Regex
Defined in:
lib/cl/help/cmd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Regex

#format_regex

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd

Returns:

  • (Object)

    the current value of cmd



6
7
8
# File 'lib/cl/help/cmd.rb', line 6

def cmd
  @cmd
end

#ctxObject

Returns the value of attribute ctx

Returns:

  • (Object)

    the current value of ctx



6
7
8
# File 'lib/cl/help/cmd.rb', line 6

def ctx
  @ctx
end

Instance Method Details

#argsObject



46
47
48
49
50
# File 'lib/cl/help/cmd.rb', line 46

def args
  @args ||= begin
    Table.new(cmd.args.map { |arg| [arg.name, format_obj(arg)] })
  end
end

#argumentsObject



25
26
27
# File 'lib/cl/help/cmd.rb', line 25

def arguments
  ['Arguments:', table(:args)] if args.any?
end

#cmmnObject



63
64
65
66
67
68
69
70
71
# File 'lib/cl/help/cmd.rb', line 63

def cmmn
  @cmmn ||= begin
    opts = cmd.superclass.opts
    opts = opts.reject(&:internal?)
    strs = Table.new(rjust(opts.map { |opt| [*opt.strs] }))
    opts = opts.map { |opt| format_obj(opt) }
    Table.new(strs.rows.zip(opts))
  end
end

#commonObject



33
34
35
# File 'lib/cl/help/cmd.rb', line 33

def common
  ['Common Options:', table(:cmmn)] if common?
end

#common?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cl/help/cmd.rb', line 81

def common?
  cmd.superclass < Cl::Cmd
end

#descriptionObject



21
22
23
# File 'lib/cl/help/cmd.rb', line 21

def description
  ['Description:', indent(cmd.description)] if cmd.description
end

#examplesObject



37
38
39
# File 'lib/cl/help/cmd.rb', line 37

def examples
  ['Examples:', indent(cmd.examples)] if cmd.examples
end

#formatObject



9
10
11
# File 'lib/cl/help/cmd.rb', line 9

def format
  [usage, summary, description, arguments, options, common, examples].compact.join("\n\n")
end

#format_aliases(opt) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/cl/help/cmd.rb', line 117

def format_aliases(opt)
  opt.aliases.map do |name|
    strs = [name]
    strs << "(deprecated, please use #{opt.name})" if opt.deprecated[0] == name
    strs.join(' ')
  end.join(', ')
end

#format_default(opt) ⇒ Object



134
135
136
# File 'lib/cl/help/cmd.rb', line 134

def format_default(opt)
  opt.default.is_a?(Symbol) ? opt.default.to_s.sub('_', ' ') : opt.default
end

#format_deprecated(opt) ⇒ Object



138
139
140
# File 'lib/cl/help/cmd.rb', line 138

def format_deprecated(opt)
  return "deprecated (#{opt.deprecated[1]})" if opt.deprecated[0] == opt.name
end

#format_enum(opt) ⇒ Object



125
126
127
# File 'lib/cl/help/cmd.rb', line 125

def format_enum(opt)
  opt.enum.map { |value| format_regex(value) }.join(', ')
end

#format_obj(obj) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/cl/help/cmd.rb', line 89

def format_obj(obj)
  opts = []
  opts << "type: #{format_type(obj)}" unless obj.type == :flag
  opts << 'required: true' if obj.required?
  opts += format_opt(obj) if obj.is_a?(Opt)
  opts = opts.join(', ')
  opts = "(#{opts})" if obj.description && !opts.empty?
  opts = [obj.description, opts]
  opts.compact.map(&:strip).join(' ')
end

#format_opt(opt) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cl/help/cmd.rb', line 100

def format_opt(opt)
  opts = []
  opts << "alias: #{format_aliases(opt)}" if opt.aliases?
  opts << "requires: #{opt.requires.join(', ')}" if opt.requires?
  opts << "default: #{format_default(opt)}" if opt.default?
  opts << "known values: #{format_enum(opt)}" if opt.enum?
  opts << "format: #{opt.format}" if opt.format?
  opts << "downcase: true" if opt.downcase?
  opts << "min: #{opt.min}" if opt.min?
  opts << "max: #{opt.max}" if opt.max?
  opts << "e.g.: #{opt.example}" if opt.example?
  opts << "note: #{opt.note}" if opt.note?
  opts << "see: #{opt.see}" if opt.see?
  opts << format_deprecated(opt) if opt.deprecated?
  opts.compact
end

#format_type(obj) ⇒ Object



129
130
131
132
# File 'lib/cl/help/cmd.rb', line 129

def format_type(obj)
  return obj.type unless obj.is_a?(Opt) && obj.type == :array
  "array (string, can be given multiple times)"
end

#indent(str) ⇒ Object



148
149
150
# File 'lib/cl/help/cmd.rb', line 148

def indent(str)
  str.lines.map { |line| "  #{line}" }.join
end

#optionsObject



29
30
31
# File 'lib/cl/help/cmd.rb', line 29

def options
  ['Options:', requireds, table(:opts)].compact if opts.any?
end

#optsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/cl/help/cmd.rb', line 52

def opts
  @opts ||= begin
    opts = cmd.opts.to_a
    opts = opts.reject(&:internal?)
    opts = opts - cmd.superclass.opts.to_a if common?
    strs = Table.new(rjust(opts.map { |opt| [*opt.strs] }))
    opts = opts.map { |opt| format_obj(opt) }
    Table.new(strs.rows.zip(opts))
  end
end

#requiredsObject



73
74
75
76
77
78
79
# File 'lib/cl/help/cmd.rb', line 73

def requireds
  return unless cmd.required?
  opts = cmd.required
  strs = opts.map { |alts| alts.map { |alt| Array(alt).join(' and ') }.join(', or ' ) }
  strs = strs.map { |str| "Either #{str} are required." }.join("\n")
  indent(strs) unless strs.empty?
end

#rjust(objs) ⇒ Object



142
143
144
145
146
# File 'lib/cl/help/cmd.rb', line 142

def rjust(objs)
  return objs unless objs.any?
  width = objs.max_by(&:size).size
  objs.map { |objs| [*Array.new(width - objs.size) { '' }, *objs] }
end

#summaryObject



17
18
19
# File 'lib/cl/help/cmd.rb', line 17

def summary
  ['Summary:', indent(cmd.summary)] if cmd.summary
end

#table(name) ⇒ Object



41
42
43
44
# File 'lib/cl/help/cmd.rb', line 41

def table(name)
  table = send(name)
  indent(table.to_s(width - table.width + 5))
end

#usageObject



13
14
15
# File 'lib/cl/help/cmd.rb', line 13

def usage
  "Usage: #{Usage.new(ctx, cmd).format}"
end

#widthObject



85
86
87
# File 'lib/cl/help/cmd.rb', line 85

def width
  [args.width, opts.width, cmmn.width].max
end