Class: Cl::Help::Cmd

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Format

#format_obj

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd

Returns:

  • (Object)

    the current value of cmd



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

def cmd
  @cmd
end

#ctxObject

Returns the value of attribute ctx

Returns:

  • (Object)

    the current value of ctx



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

def ctx
  @ctx
end

Instance Method Details

#argsObject



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

#argumentsObject



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

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

#cmmnObject



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

#commonObject



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

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

#common?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/cl/help/cmd.rb', line 99

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

#descriptionObject



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

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

#examplesObject



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

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

#formatObject



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

def format
  [usage, summary, description, arguments, options, 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

Returns:

  • (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

#optionsObject



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

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

#optsObject



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

#requiredsObject



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

#summaryObject



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

#usageObject



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

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

#widthObject



103
104
105
# File 'lib/cl/help/cmd.rb', line 103

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