Class: Cl::Help::Cmd

Inherits:
Object
  • 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

Constructor Details

#initialize(cmd) ⇒ Cmd

Returns a new instance of Cmd.


11
12
13
# File 'lib/cl/help/cmd.rb', line 11

def initialize(cmd)
  @cmd = cmd
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd


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

def cmd
  @cmd
end

Instance Method Details

#argsObject


52
53
54
55
56
# File 'lib/cl/help/cmd.rb', line 52

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

#argumentsObject


31
32
33
# File 'lib/cl/help/cmd.rb', line 31

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

#cmmnObject


69
70
71
72
73
74
75
76
77
# File 'lib/cl/help/cmd.rb', line 69

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


39
40
41
# File 'lib/cl/help/cmd.rb', line 39

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

#common?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/cl/help/cmd.rb', line 87

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

#descriptionObject


27
28
29
# File 'lib/cl/help/cmd.rb', line 27

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

#examplesObject


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

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

#formatObject


15
16
17
# File 'lib/cl/help/cmd.rb', line 15

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

#format_aliases(opt) ⇒ Object


122
123
124
125
126
127
128
# File 'lib/cl/help/cmd.rb', line 122

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


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

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

#format_deprecated(opt) ⇒ Object


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

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

#format_enum(opt) ⇒ Object


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

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

#format_obj(obj) ⇒ Object


95
96
97
98
99
100
101
102
103
104
# File 'lib/cl/help/cmd.rb', line 95

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


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cl/help/cmd.rb', line 106

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 << "see: #{opt.see}" if opt.see?
  opts << format_deprecated(opt) if opt.deprecated?
  opts.compact
end

#format_type(obj) ⇒ Object


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

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


152
153
154
# File 'lib/cl/help/cmd.rb', line 152

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

#optionsObject


35
36
37
# File 'lib/cl/help/cmd.rb', line 35

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

#optsObject


58
59
60
61
62
63
64
65
66
67
# File 'lib/cl/help/cmd.rb', line 58

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


79
80
81
82
83
84
85
# File 'lib/cl/help/cmd.rb', line 79

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


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

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

#summaryObject


23
24
25
# File 'lib/cl/help/cmd.rb', line 23

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

#table(name) ⇒ Object


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

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

#usageObject


19
20
21
# File 'lib/cl/help/cmd.rb', line 19

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

#widthObject


91
92
93
# File 'lib/cl/help/cmd.rb', line 91

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