Class: Cl::Help::Cmd
- Inherits:
-
Struct
- Object
- Struct
- Cl::Help::Cmd
- Includes:
- Regex
- Defined in:
- lib/cl/help/cmd.rb
Instance Attribute Summary collapse
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#ctx ⇒ Object
Returns the value of attribute ctx.
Instance Method Summary collapse
- #args ⇒ Object
- #arguments ⇒ Object
- #cmmn ⇒ Object
- #common ⇒ Object
- #common? ⇒ Boolean
- #description ⇒ Object
- #examples ⇒ Object
- #format ⇒ Object
- #format_aliases(opt) ⇒ Object
- #format_default(opt) ⇒ Object
- #format_deprecated(opt) ⇒ Object
- #format_enum(opt) ⇒ Object
- #format_obj(obj) ⇒ Object
- #format_opt(opt) ⇒ Object
- #format_type(obj) ⇒ Object
- #indent(str) ⇒ Object
- #options ⇒ Object
- #opts ⇒ Object
- #requireds ⇒ Object
- #rjust(objs) ⇒ Object
- #summary ⇒ Object
- #table(name) ⇒ Object
- #usage ⇒ Object
- #width ⇒ Object
Methods included from Regex
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd
6 7 8 |
# File 'lib/cl/help/cmd.rb', line 6 def cmd @cmd end |
#ctx ⇒ Object
Returns the value of attribute ctx
6 7 8 |
# File 'lib/cl/help/cmd.rb', line 6 def ctx @ctx end |
Instance Method Details
#args ⇒ Object
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 |
#arguments ⇒ Object
25 26 27 |
# File 'lib/cl/help/cmd.rb', line 25 def arguments ['Arguments:', table(:args)] if args.any? end |
#cmmn ⇒ Object
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 |
#common ⇒ Object
33 34 35 |
# File 'lib/cl/help/cmd.rb', line 33 def common ['Common Options:', table(:cmmn)] if common? end |
#common? ⇒ Boolean
81 82 83 |
# File 'lib/cl/help/cmd.rb', line 81 def common? cmd.superclass < Cl::Cmd end |
#description ⇒ Object
21 22 23 |
# File 'lib/cl/help/cmd.rb', line 21 def description ['Description:', indent(cmd.description)] if cmd.description end |
#examples ⇒ Object
37 38 39 |
# File 'lib/cl/help/cmd.rb', line 37 def examples ['Examples:', indent(cmd.examples)] if cmd.examples end |
#format ⇒ Object
9 10 11 |
# File 'lib/cl/help/cmd.rb', line 9 def format [usage, summary, description, arguments, , 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 |
#options ⇒ Object
29 30 31 |
# File 'lib/cl/help/cmd.rb', line 29 def ['Options:', requireds, table(:opts)].compact if opts.any? end |
#opts ⇒ Object
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 |
#requireds ⇒ Object
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 |
#summary ⇒ Object
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 |
#usage ⇒ Object
13 14 15 |
# File 'lib/cl/help/cmd.rb', line 13 def usage "Usage: #{Usage.new(ctx, cmd).format}" end |
#width ⇒ Object
85 86 87 |
# File 'lib/cl/help/cmd.rb', line 85 def width [args.width, opts.width, cmmn.width].max end |