File: basic_help.rb

package info (click to toggle)
ruby-cri 2.15.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: ruby: 2,776; makefile: 11
file content (27 lines) | stat: -rw-r--r-- 840 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

name        'help'
usage       'help [command_name]'
summary     'show help'
description <<~DESC
  Show help for the given command, or show general help. When no command is
  given, a list of available commands is displayed, as well as a list of global
  command-line options. When a command is given, a command description, as well
  as command-specific command-line options, are shown.
DESC

flag :v, :verbose, 'show more detailed help'

run do |opts, args, cmd|
  if cmd.supercommand.nil?
    raise NoHelpAvailableError,
          'No help available because the help command has no supercommand'
  end

  is_verbose = opts.fetch(:verbose, false)

  resolved_cmd = args.inject(cmd.supercommand) do |acc, name|
    acc.command_named(name)
  end
  puts resolved_cmd.help(verbose: is_verbose, io: $stdout)
end