Module: Simple::CLI::Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/simple/cli/helper.rb,
lib/simple/cli/helper/help.rb,
lib/simple/cli/helper/short_help.rb,
lib/simple/cli/helper/help_on_command.rb

Instance Method Summary collapse

Instance Method Details

#help!(service, verbose:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/simple/cli/helper/help.rb', line 3

def help!(service, verbose:)
  STDERR.puts <<~MSG
    #{H.binary_name} <subcommand> [ options... ]

    Subcommands:

    #{format_usages usages(service, verbose: verbose), prefix: "    "}

    Default options and subcommands include:

    #{format_usages default_usages(service, verbose: verbose), prefix: "    "}

  MSG

  exit 2
end

#help_on_command!(service, command, verbose:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple/cli/helper/help_on_command.rb', line 7

def help_on_command!(service, command, verbose:)
  _ = verbose
  action = H.action_for_command(service, command)

  parts = [
    action.short_description,
    action_usage(action),
    action.full_description,
  ].compact

  STDERR.puts <<~MSG
    #{parts.join("\n\n")}

  MSG

  exit 2
end

#short_help!(service) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple/cli/helper/short_help.rb', line 5

def short_help!(service)
  # We check if we have only a few number of actions. In that case we just show the full help instead.
  actions = ::Simple::Service.actions(service).values
  actions, hidden_actions = actions.partition(&:short_description)

  STDERR.puts <<~MSG
    #{H.binary_name} <subcommand> [ options... ]

  MSG

  # if we don't have too many subcommands we print them here. If not, we only print their names
  # and mention the help command.
  if actions.count < 8
    STDERR.puts <<~MSG
      Subcommands:

      #{format_usages usages(service, verbose: false), prefix: "    "}

    MSG
  else
    subcommands = actions.map { |action| "'" + H.action_to_command(action.name) + "'" }
    msg = "Subcommands include #{subcommands.sort.join(", ")}"
    msg += " (and, in addition, #{hidden_actions.count} internal commands)" if hidden_actions.count > 0

    STDERR.puts <<~MSG
      #{msg}. Run with "-h" for more details.

    MSG
  end

  STDERR.puts <<~MSG
    Default options and subcommands include:

    #{format_usages default_usages(service, verbose: false), prefix: "    "}

    MSG

  exit 2
end