dry-cli 1.4.0 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +397 -3
- data/README.md +6 -1
- data/dry-cli.gemspec +3 -4
- data/lib/dry/cli/ansi.rb +175 -0
- data/lib/dry/cli/banner.rb +122 -70
- data/lib/dry/cli/command.rb +448 -38
- data/lib/dry/cli/command_registry.rb +3 -1
- data/lib/dry/cli/dispatch.rb +55 -0
- data/lib/dry/cli/errors.rb +106 -0
- data/lib/dry/cli/inline.rb +2 -2
- data/lib/dry/cli/namespace.rb +125 -0
- data/lib/dry/cli/option.rb +84 -47
- data/lib/dry/cli/parser.rb +116 -40
- data/lib/dry/cli/registry.rb +117 -4
- data/lib/dry/cli/spinner/dsl.rb +82 -0
- data/lib/dry/cli/spinner.rb +122 -0
- data/lib/dry/cli/stream.rb +127 -0
- data/lib/dry/cli/style/attribute.rb +47 -0
- data/lib/dry/cli/style/color.rb +196 -0
- data/lib/dry/cli/style/color_level.rb +114 -0
- data/lib/dry/cli/style/formatter.rb +132 -0
- data/lib/dry/cli/style/palette.rb +162 -0
- data/lib/dry/cli/style/text.rb +232 -0
- data/lib/dry/cli/style.rb +620 -0
- data/lib/dry/cli/style_mixin.rb +54 -0
- data/lib/dry/cli/ticker.rb +57 -0
- data/lib/dry/cli/usage.rb +23 -16
- data/lib/dry/cli/version.rb +1 -1
- data/lib/dry/cli.rb +155 -38
- metadata +18 -4
data/lib/dry/cli/banner.rb
CHANGED
|
@@ -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
|
-
#
|
|
11
|
+
# An entry in a banner section: a label and its description.
|
|
13
12
|
#
|
|
14
|
-
# @
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
]
|
|
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
|
|
53
|
+
# @since 1.1.1
|
|
32
54
|
# @api private
|
|
33
|
-
def self.
|
|
34
|
-
|
|
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.
|
|
40
|
-
|
|
69
|
+
def self.command_name(name, label = "Command")
|
|
70
|
+
"#{label}:\n #{name}"
|
|
71
|
+
end
|
|
41
72
|
|
|
42
|
-
|
|
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
|
-
|
|
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.
|
|
50
|
-
return if
|
|
83
|
+
def self.section(heading, entries, indent)
|
|
84
|
+
return if entries.empty?
|
|
51
85
|
|
|
52
|
-
"\
|
|
86
|
+
"\n#{heading}:\n#{entries.map { |entry| entry.render(indent) }.join("\n")}"
|
|
53
87
|
end
|
|
54
88
|
|
|
55
|
-
#
|
|
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
|
-
|
|
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
|
|
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#{
|
|
109
|
+
"\nSubcommands:\n#{subcommands_list(command.subcommands)}"
|
|
67
110
|
end
|
|
68
111
|
|
|
69
|
-
# @since 0.1.0
|
|
70
112
|
# @api private
|
|
71
|
-
def self.
|
|
72
|
-
|
|
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
|
-
"
|
|
118
|
+
" #{args.join(" ")}" unless args.empty?
|
|
75
119
|
end
|
|
76
120
|
|
|
77
|
-
# @since 0.1.0
|
|
78
121
|
# @api private
|
|
79
|
-
def self.
|
|
80
|
-
|
|
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.
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
result =
|
|
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
|
-
|
|
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.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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.
|
|
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
|