Module: Dry::CLI::Banner Private

Defined in:
lib/dry/cli/banner.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 banner

Since:

  • 0.1.0

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



85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/cli/banner.rb', line 85

def self.arguments(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 Metrics/LineLength
  optional = optional_arguments.map { |arg| "[#{arg.name.upcase}]" }.join(" ") if optional_arguments.any? # rubocop:disable Metrics/LineLength
  result = [required, optional].compact

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

.build_subcommands_list(subcommands) ⇒ 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



128
129
130
131
132
# File 'lib/dry/cli/banner.rb', line 128

def self.build_subcommands_list(subcommands)
  subcommands.map do |subcommand_name, subcommand|
    "  #{subcommand_name.ljust(32)}  # #{subcommand.command.description}"
  end.join("\n")
end

.call(command, 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.

Prints command banner

Parameters:

Since:

  • 0.1.0



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

def self.call(command, name)
  [
    command_name(name),
    command_name_and_arguments(command, name),
    command_description(command),
    command_subcommands(command),
    command_arguments(command),
    command_options(command),
    command_examples(command, name)
  ].compact.join("\n")
end

.command_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



71
72
73
74
75
# File 'lib/dry/cli/banner.rb', line 71

def self.command_arguments(command)
  return if command.arguments.empty?

  "\nArguments:\n#{extended_command_arguments(command)}"
end

.command_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



57
58
59
60
61
# File 'lib/dry/cli/banner.rb', line 57

def self.command_description(command)
  return if command.description.nil?

  "\nDescription:\n  #{command.description}"
end

.command_examples(command, 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



49
50
51
52
53
# File 'lib/dry/cli/banner.rb', line 49

def self.command_examples(command, name)
  return if command.examples.empty?

  "\nExamples:\n#{command.examples.map { |example| "  #{name} #{example}" }.join("\n")}"
end

.command_name(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



33
34
35
# File 'lib/dry/cli/banner.rb', line 33

def self.command_name(name)
  "Command:\n  #{name}"
end

.command_name_and_arguments(command, 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



39
40
41
42
43
44
45
# File 'lib/dry/cli/banner.rb', line 39

def self.command_name_and_arguments(command, name)
  usage = "\nUsage:\n  #{name}#{arguments(command)}"

  return usage + " | #{name} SUBCOMMAND" if command.subcommands.any?

  usage
end

.command_options(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



79
80
81
# File 'lib/dry/cli/banner.rb', line 79

def self.command_options(command)
  "\nOptions:\n#{extended_command_options(command)}"
end

.command_subcommands(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



63
64
65
66
67
# File 'lib/dry/cli/banner.rb', line 63

def self.command_subcommands(command)
  return if command.subcommands.empty?

  "\nSubcommands:\n#{build_subcommands_list(command.subcommands)}"
end

.extended_command_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



98
99
100
101
102
# File 'lib/dry/cli/banner.rb', line 98

def self.extended_command_arguments(command)
  command.arguments.map do |argument|
    "  #{argument.name.to_s.upcase.ljust(32)}  # #{"REQUIRED " if argument.required?}#{argument.desc}" # rubocop:disable Metrics/LineLength
  end.join("\n")
end

.extended_command_options(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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dry/cli/banner.rb', line 107

def self.extended_command_options(command)
  result = command.options.map do |option|
    name = Inflector.dasherize(option.name)
    name = if option.boolean?
             "[no-]#{name}"
           elsif option.array?
             "#{name}=VALUE1,VALUE2,.."
           else
             "#{name}=VALUE"
           end
    name = "#{name}, #{option.alias_names.join(", ")}" if option.aliases.any?
    name = "  --#{name.ljust(30)}"
    name = "#{name}  # #{option.desc}"
    name = "#{name}, default: #{option.default.inspect}" unless option.default.nil?
    name
  end

  result << "  --#{"help, -h".ljust(30)}  # Print this help"
  result.join("\n")
end