Module: Dry::CLI::Usage Private

Defined in:
lib/dry/cli/usage.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Command(s) usage

Since:

  • 0.1.0

Constant Summary collapse

SUBCOMMAND_BANNER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

" [SUBCOMMAND]"
ROOT_COMMAND_WITH_SUBCOMMANDS_BANNER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

" [ARGUMENT|SUBCOMMAND]"

Class Method Summary collapse

Class Method Details

.arguments(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dry/cli/usage.rb', line 54

def self.arguments(command)
  return unless CLI.command?(command)

  required_arguments = command.required_arguments
  optional_arguments = command.optional_arguments

  required = required_arguments.map { |arg| arg.name.upcase }.join(" ") if required_arguments.any? # rubocop:disable Layout/LineLength
  optional = optional_arguments.map { |arg| "[#{arg.name.upcase}]" }.join(" ") if optional_arguments.any? # rubocop:disable Layout/LineLength
  result = [required, optional].compact

  " #{result.join(" ")}" unless result.empty?
end

.call(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dry/cli/usage.rb', line 19

def self.call(result)
  header = "Commands:"
  max_length, commands = commands_and_arguments(result)

  commands.map do |banner, node|
    next if node.hidden

    usage = description(node.command) if node.leaf?
    "#{justify(banner, max_length, usage)}#{usage}"
  end.compact.unshift(header).join("\n")
end

.command_name(result, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



91
92
93
# File 'lib/dry/cli/usage.rb', line 91

def self.command_name(result, name)
  ProgramName.call([result.names, name])
end

.commands(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



85
86
87
# File 'lib/dry/cli/usage.rb', line 85

def self.commands(result)
  result.children.sort_by { |name, _| name }
end

.commands_and_arguments(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dry/cli/usage.rb', line 33

def self.commands_and_arguments(result)
  max_length = 0
  ret        = commands(result).each_with_object({}) do |(name, node), memo|
    args = if node.command && node.leaf? && node.children?
             ROOT_COMMAND_WITH_SUBCOMMANDS_BANNER
           elsif node.leaf?
             arguments(node.command)
           else
             SUBCOMMAND_BANNER
           end

    partial       = "  #{command_name(result, name)}#{args}"
    max_length    = partial.bytesize if max_length < partial.bytesize
    memo[partial] = node
  end

  [max_length, ret]
end

.description(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



69
70
71
72
73
# File 'lib/dry/cli/usage.rb', line 69

def self.description(command)
  return unless CLI.command?(command)

  " # #{command.description}" unless command.description.nil?
end

.justify(string, padding, usage) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



77
78
79
80
81
# File 'lib/dry/cli/usage.rb', line 77

def self.justify(string, padding, usage)
  return string.chomp(" ") if usage.nil?

  string.ljust(padding + padding / 2)
end