283
283
parser = Cri::OptionParser.new(
284
284
opts_and_args, self.global_option_definitions)
285
self.handle_parser_errors_while { parser.run }
285
handle_parser_errors_while { parser.run }
286
286
local_opts = parser.options
287
287
global_opts = parent_opts.merge(parser.options)
288
288
args = parser.arguments
291
self.handle_options(local_opts)
291
handle_options(local_opts)
294
294
if self.block.nil?
301
301
# @return [String] The help text for this command
303
# @option params [Boolean] :verbose true if the help output should be
304
# verbose, false otherwise.
302
305
def help(params={})
303
is_verbose = params.fetch(:verbose, false)
307
# Append name and summary
309
text << "name".formatted_as_title << "\n"
310
text << " #{name.formatted_as_command} - #{summary}" << "\n"
311
unless aliases.empty?
312
text << " aliases: " << aliases.map { |a| a.formatted_as_command }.join(' ') << "\n"
318
path = [ self.supercommand ]
319
path.unshift(path[0].supercommand) until path[0].nil?
320
formatted_usage = usage.gsub(/^([^\s]+)/) { |m| m.formatted_as_command }
321
full_usage = path[1..-1].map { |c| c.name.formatted_as_command + ' ' }.join + formatted_usage
324
text << "usage".formatted_as_title << "\n"
325
text << full_usage.wrap_and_indent(78, 4) << "\n"
328
# Append long description
331
text << "description".formatted_as_title << "\n"
332
text << description.wrap_and_indent(78, 4) + "\n"
336
unless self.subcommands.empty?
338
text << (self.supercommand ? 'subcommands' : 'commands').formatted_as_title
341
shown_subcommands = self.subcommands.select { |c| !c.hidden? || is_verbose }
342
length = shown_subcommands.map { |c| c.name.formatted_as_command.size }.max
345
shown_subcommands.sort_by { |cmd| cmd.name }.each do |cmd|
346
text << sprintf(" %-#{length+4}s %s\n",
347
cmd.name.formatted_as_command,
353
diff = self.subcommands.size - shown_subcommands.size
357
text << " (1 hidden command omitted; show it with --verbose)\n"
359
text << " (#{diff} hidden commands omitted; show them with --verbose)\n"
365
groups = { 'options' => self.option_definitions }
367
groups["options for #{self.supercommand.name}"] = self.supercommand.global_option_definitions
369
length = groups.values.inject(&:+).map { |o| o[:long].to_s.size }.max
370
groups.keys.sort.each do |name|
374
text << "#{name}".formatted_as_title
376
ordered_defs = defs.sort_by { |x| x[:short] || x[:long] }
377
ordered_defs.each do |opt_def|
379
" %-2s %-#{length+6}s",
380
opt_def[:short] ? ('-' + opt_def[:short]) : '',
381
opt_def[:long] ? ('--' + opt_def[:long]) : '').formatted_as_option
383
text << opt_def[:desc] << "\n"
306
HelpRenderer.new(self, params).render
391
309
# Compares this command's name to the other given command's name.
311
# @param [Cri::Command] other The command to compare with
313
# @return [-1, 0, 1] The result of the comparison between names
393
317
self.name <=> other.name
398
322
def handle_options(opts)
399
323
opts.each_pair do |key, value|