45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/optitron/help.rb', line 45
def generate
cmds = []
@parser.commands.each do |(cmd_name, cmd)|
cmd_line = "#{cmd_name}"
cmd.args.each do |arg|
cmd_line << " " << help_line_for_arg(arg)
end
cmds << [cmd_line, cmd]
cmd.options.each do |opt|
cmds.assoc(cmd_line) << help_line_for_opt(opt)
end
end
cmds.sort!{ |cmd1, cmd2| (cmd1[1].group || '') <=> (cmd2[1].group || '') }
opts_lines = @parser.options.map { |opt| help_line_for_opt(opt) }
args_lines = @parser.args.empty? ? nil : [@parser.args.map{|arg| help_line_for_arg(arg)}.join(' '), @parser.args.map{|arg| arg.desc}.join(', ')]
longest_line = 0
longest_line = [longest_line, cmds.map{|cmd| cmd.first.size}.max].max unless cmds.empty?
opt_lines = cmds.map{|k,v| k.size + 2}.flatten
longest_line = [longest_line, args_lines.first.size].max if args_lines
longest_line = [longest_line, opt_lines.max].max unless opt_lines.empty?
longest_line = [opts_lines.map{|o| o.first.size}.max, longest_line].max unless opts_lines.empty?
help_output = []
last_group = nil
unless cmds.empty?
help_output << "Commands\n\n" + cmds.map do |(cmd, *opts)|
cmd_text = ""
cmd_obj = opts.shift
if last_group != cmd_obj.group
cmd_text << "#{cmd_obj.group}:\n"
last_group = cmd_obj.group
end
cmd_text << "%-#{longest_line}s " % cmd
cmd_text << "# #{cmd_obj.desc}" if cmd_obj.desc
cmd_obj.args.each do |arg|
if arg.desc
cmd_text << "\n%-#{longest_line}s " % ""
cmd_text << "# #{arg.name} -- #{arg.desc}"
end
end
opts.each do |opt|
cmd_text << "\n %-#{longest_line}s " % opt.first
cmd_text << "# #{opt.last}" if opt.last
end
cmd_text
end.join("\n")
end
if args_lines
arg_help = "Arguments\n\n"
arg_help << "%-#{longest_line}s " % args_lines.first
arg_help << "# #{args_lines.last}" if args_lines.first
help_output << arg_help
end
unless opts_lines.empty?
help_output << "Global options\n\n" + opts_lines.map do |opt|
opt_text = ''
opt_text << "%-#{longest_line}s " % opt.first
opt_text << "# #{opt.last}" if opt.last
opt_text
end.join("\n")
end
help_output.join("\n\n")
end
|