Class: Cl::Help::Cmd
- Inherits:
-
Struct
- Object
- Struct
- Cl::Help::Cmd
- Includes:
- Format
- 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
- #indent(str) ⇒ Object
- #negate(opt) ⇒ Object
- #negate?(opt) ⇒ Boolean
- #opt_strs(opt) ⇒ Object
- #options ⇒ Object
- #opts ⇒ Object
- #requireds ⇒ Object
- #rjust(objs) ⇒ Object
- #summary ⇒ Object
- #table(name) ⇒ Object
- #usage ⇒ Object
- #width ⇒ Object
Methods included from Format
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd
7 8 9 |
# File 'lib/cl/help/cmd.rb', line 7 def cmd @cmd end |
#ctx ⇒ Object
Returns the value of attribute ctx
7 8 9 |
# File 'lib/cl/help/cmd.rb', line 7 def ctx @ctx end |
Instance Method Details
#args ⇒ Object
47 48 49 50 51 |
# File 'lib/cl/help/cmd.rb', line 47 def args @args ||= begin Table.new(cmd.args.map { |arg| [arg.name, format_obj(arg)] }) end end |
#arguments ⇒ Object
26 27 28 |
# File 'lib/cl/help/cmd.rb', line 26 def arguments ['Arguments:', table(:args)] if args.any? end |
#cmmn ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/cl/help/cmd.rb', line 64 def cmmn @cmmn ||= begin opts = cmd.superclass.opts opts = opts.reject(&:internal?) strs = Table.new(rjust(opts.map(&:strs))) opts = opts.map { |opt| format_obj(opt) } Table.new(strs.rows.zip(opts)) end end |
#common ⇒ Object
34 35 36 |
# File 'lib/cl/help/cmd.rb', line 34 def common ['Common Options:', table(:cmmn)] if common? end |
#common? ⇒ Boolean
99 100 101 |
# File 'lib/cl/help/cmd.rb', line 99 def common? cmd.superclass < Cl::Cmd end |
#description ⇒ Object
22 23 24 |
# File 'lib/cl/help/cmd.rb', line 22 def description ['Description:', indent(cmd.description)] if cmd.description end |
#examples ⇒ Object
38 39 40 |
# File 'lib/cl/help/cmd.rb', line 38 def examples ['Examples:', indent(cmd.examples)] if cmd.examples end |
#format ⇒ Object
10 11 12 |
# File 'lib/cl/help/cmd.rb', line 10 def format [usage, summary, description, arguments, , common, examples].compact.join("\n\n") end |
#indent(str) ⇒ Object
113 114 115 |
# File 'lib/cl/help/cmd.rb', line 113 def indent(str) str.lines.map { |line| " #{line}".rstrip }.join("\n") end |
#negate(opt) ⇒ Object
86 87 88 89 |
# File 'lib/cl/help/cmd.rb', line 86 def negate(opt) negations = opt.negate.map { |str| "#{str}-" }.join('|') opt.long.dup.insert(2, "[#{negations}]") end |
#negate?(opt) ⇒ Boolean
81 82 83 84 |
# File 'lib/cl/help/cmd.rb', line 81 def negate?(opt) negations = opt.negate.map { |str| "#{str}-" }.join('|') opt.long && opt.negate? && opt.long !~ /\[#{negations}\]/ end |
#opt_strs(opt) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/cl/help/cmd.rb', line 74 def opt_strs(opt) return opt.strs if !opt.flag? || opt.help? opts = [opt.short] opts.push(negate?(opt) ? negate(opt) : opt.long) opts.compact end |
#options ⇒ Object
30 31 32 |
# File 'lib/cl/help/cmd.rb', line 30 def ['Options:', requireds, table(:opts)].compact if opts.any? end |
#opts ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cl/help/cmd.rb', line 53 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(opt) })) opts = opts.map { |opt| format_obj(opt) } Table.new(strs.rows.zip(opts)) end end |
#requireds ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/cl/help/cmd.rb', line 91 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
107 108 109 110 111 |
# File 'lib/cl/help/cmd.rb', line 107 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
18 19 20 |
# File 'lib/cl/help/cmd.rb', line 18 def summary ['Summary:', indent(cmd.summary)] if cmd.summary end |
#table(name) ⇒ Object
42 43 44 45 |
# File 'lib/cl/help/cmd.rb', line 42 def table(name) table = send(name) indent(table.to_s(width - table.width + 5)) end |
#usage ⇒ Object
14 15 16 |
# File 'lib/cl/help/cmd.rb', line 14 def usage "Usage: #{Usage.new(ctx, cmd).format.join("\n or: ")}" end |
#width ⇒ Object
103 104 105 |
# File 'lib/cl/help/cmd.rb', line 103 def width [args.width, opts.width, cmmn.width].max end |