dry-cli 1.4.1 → 2.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,132 +6,184 @@ module Dry
6
6
  class CLI
7
7
  # Command banner
8
8
  #
9
- # @since 0.1.0
10
9
  # @api private
11
10
  module Banner
12
- # Prints command banner
11
+ # An entry in a banner section: a label and its description.
13
12
  #
14
- # @param command [Dry::CLI::Command] the command
13
+ # @api private
14
+ Entry = Data.define(:label, :description) do
15
+ def render(indent) = " #{label.ljust(indent)} # #{description}"
16
+ end
17
+
18
+ # Prints command/namespace banner
19
+ #
20
+ # @param command [Dry::CLI::Command, Dry::CLI::Namespace] the command/namespace
15
21
  # @param out [IO] standard output
16
22
  #
17
- # @since 0.1.0
18
23
  # @api private
19
- def self.call(command, name)
24
+ def self.call(command, name, long: false)
25
+ banner_lines =
26
+ if CLI.command?(command)
27
+ command_banner(command, name, long)
28
+ else
29
+ namespace_banner(command, name, long)
30
+ end
31
+
32
+ banner_lines.compact.join("\n")
33
+ end
34
+
35
+ # @api private
36
+ def self.command_banner(command, name, long = false)
37
+ argument_entries = command_argument_entries(command)
38
+ example_entries = command_example_entries(command, name)
39
+ option_entries = command_option_entries(command)
40
+ indent = capture_indent(argument_entries + option_entries + example_entries)
41
+
20
42
  [
21
43
  command_name(name),
22
44
  command_name_and_arguments(command, name),
23
- command_description(command),
45
+ command_description(command, long),
24
46
  command_subcommands(command),
25
- command_arguments(command),
26
- command_options(command),
27
- command_examples(command, name)
28
- ].compact.join("\n")
47
+ section("Arguments", argument_entries, indent),
48
+ section("Options", option_entries, indent),
49
+ section("Examples", example_entries, indent)
50
+ ]
29
51
  end
30
52
 
31
- # @since 0.1.0
53
+ # @since 1.1.1
32
54
  # @api private
33
- def self.command_name(name)
34
- "Command:\n #{name}"
55
+ def self.namespace_banner(namespace, name, long = false)
56
+ option_entries = command_option_entries(namespace)
57
+ indent = capture_indent(option_entries)
58
+
59
+ [
60
+ command_name(name, "Namespace"),
61
+ command_name_and_arguments(namespace, name),
62
+ command_description(namespace, long),
63
+ command_subcommands(namespace),
64
+ section("Options", option_entries, indent)
65
+ ]
35
66
  end
36
67
 
37
- # @since 0.1.0
38
68
  # @api private
39
- def self.command_name_and_arguments(command, name)
40
- usage = "\nUsage:\n #{name}#{arguments(command)}"
69
+ def self.command_name(name, label = "Command")
70
+ "#{label}:\n #{name}"
71
+ end
41
72
 
42
- return usage + " | #{name} SUBCOMMAND" if command.subcommands.any?
73
+ # @api private
74
+ def self.command_name_and_arguments(command, name)
75
+ parts = []
76
+ parts << "#{name}#{arguments(command)}" if command.new.respond_to?(:call)
77
+ parts << "#{name} SUBCOMMAND" if command.subcommands.any?
43
78
 
44
- usage
79
+ "\nUsage:\n #{parts.join(" | ")}" unless parts.empty?
45
80
  end
46
81
 
47
- # @since 0.1.0
48
82
  # @api private
49
- def self.command_examples(command, name)
50
- return if command.examples.empty?
83
+ def self.section(heading, entries, indent)
84
+ return if entries.empty?
51
85
 
52
- "\nExamples:\n#{command.examples.map { |example| " #{name} #{example}" }.join("\n")}"
86
+ "\n#{heading}:\n#{entries.map { |entry| entry.render(indent) }.join("\n")}"
53
87
  end
54
88
 
55
- # @since 0.1.0
89
+ # Renders the "Description" section.
90
+ #
91
+ # The long description is only shown on full help (`--help`), falling back to the short
92
+ # description when it is not set. Short help (`-h`) always shows the short description.
93
+ #
56
94
  # @api private
57
- def self.command_description(command)
58
- return if command.description.nil?
95
+ def self.command_description(command, long = false)
96
+ description = (command.long_description if long) || command.description
97
+ return if description.nil?
98
+
99
+ body = description.chomp.lines.map { |line|
100
+ line.strip.empty? ? "" : " #{line.rstrip}"
101
+ }.join("\n")
59
102
 
60
- "\nDescription:\n #{command.description}"
103
+ "\nDescription:\n#{body}"
61
104
  end
62
105
 
63
106
  def self.command_subcommands(command)
64
107
  return if command.subcommands.empty?
65
108
 
66
- "\nSubcommands:\n#{build_subcommands_list(command.subcommands)}"
109
+ "\nSubcommands:\n#{subcommands_list(command.subcommands)}"
67
110
  end
68
111
 
69
- # @since 0.1.0
70
112
  # @api private
71
- def self.command_arguments(command)
72
- return if command.arguments.empty?
113
+ def self.arguments(command)
114
+ args = command.arguments_sorted_by_usage_order.map { |a|
115
+ a.required? ? a.name.to_s.upcase : "[#{a.name.to_s.upcase}]"
116
+ }
73
117
 
74
- "\nArguments:\n#{extended_command_arguments(command)}"
118
+ " #{args.join(" ")}" unless args.empty?
75
119
  end
76
120
 
77
- # @since 0.1.0
78
121
  # @api private
79
- def self.command_options(command)
80
- "\nOptions:\n#{extended_command_options(command)}"
122
+ def self.command_argument_entries(command)
123
+ command.arguments.map do |argument|
124
+ Entry.new(
125
+ label: argument.name.to_s.upcase,
126
+ description: "#{"REQUIRED " if argument.required?}#{argument.desc}"
127
+ )
128
+ end
81
129
  end
82
130
 
83
- # @since 0.1.0
84
131
  # @api private
85
- def self.arguments(command)
86
- required_arguments = command.required_arguments
87
- optional_arguments = command.optional_arguments
132
+ def self.command_example_entries(command, name)
133
+ command.examples.map do |example, description|
134
+ Entry.new(label: "#{name} #{example}", description: description)
135
+ end
136
+ end
88
137
 
89
- required = required_arguments.map { |arg| arg.name.upcase }.join(" ") if required_arguments.any?
90
- optional = optional_arguments.map { |arg| "[#{arg.name.upcase}]" }.join(" ") if optional_arguments.any?
91
- result = [required, optional].compact
138
+ # @api private
139
+ def self.command_option_entries(command)
140
+ result = command.options.map { |option|
141
+ Entry.new(label: option_label(option), description: option_description(option))
142
+ }
143
+ result << Entry.new(label: "--help, -h", description: "Print this help")
144
+ end
92
145
 
93
- " #{result.join(" ")}" unless result.empty?
146
+ # @api private
147
+ def self.option_description(option)
148
+ description = "#{"REQUIRED " if option.required?}#{option.desc}"
149
+ unless option.default.nil?
150
+ description = "#{description}, default: #{option.default.inspect}"
151
+ end
152
+ description
94
153
  end
95
154
 
96
- # @since 0.1.0
97
155
  # @api private
98
- def self.extended_command_arguments(command)
99
- command.arguments.map do |argument|
100
- " #{argument.name.to_s.upcase.ljust(32)} # #{"REQUIRED " if argument.required?}#{argument.desc}"
101
- end.join("\n")
156
+ def self.option_label(option)
157
+ label = option_usage(option)
158
+ label = "#{label}, #{option.alias_names.join(", ")}" if option.aliases.any?
159
+ label
102
160
  end
103
161
 
104
- # @since 0.1.0
105
162
  # @api private
106
- #
107
- def self.extended_command_options(command)
108
- result = command.options.map do |option|
109
- name = Inflector.dasherize(option.name)
110
- name = if option.boolean?
111
- "[no-]#{name}"
112
- elsif option.flag?
113
- name
114
- elsif option.array?
115
- "#{name}=VALUE1,VALUE2,.."
116
- else
117
- "#{name}=VALUE"
118
- end
119
- name = "#{name}, #{option.alias_names.join(", ")}" if option.aliases.any?
120
- name = " --#{name.ljust(30)}"
121
- name = "#{name} # #{option.desc}"
122
- name = "#{name}, default: #{option.default.inspect}" unless option.default.nil?
123
- name
163
+ def self.option_usage(option)
164
+ base = Inflector.dasherize(option.name)
165
+
166
+ if option.boolean?
167
+ "--[no-]#{base}"
168
+ elsif option.flag?
169
+ "--#{base}"
170
+ elsif option.array?
171
+ "--#{base}=VALUE1,VALUE2,.."
172
+ else
173
+ "--#{base}=VALUE"
124
174
  end
125
-
126
- result << " --#{"help, -h".ljust(30)} # Print this help"
127
- result.join("\n")
128
175
  end
129
176
 
130
- def self.build_subcommands_list(subcommands)
177
+ def self.subcommands_list(subcommands)
131
178
  subcommands.map do |subcommand_name, subcommand|
132
179
  " #{subcommand_name.ljust(32)} # #{subcommand.command.description}"
133
180
  end.join("\n")
134
181
  end
182
+
183
+ # @api private
184
+ def self.capture_indent(entries)
185
+ entries.map { |entry| entry.label.length }.max + 1
186
+ end
135
187
  end
136
188
  end
137
189
  end