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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +388 -2
- 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 -37
- metadata +18 -4
data/lib/dry/cli/errors.rb
CHANGED
|
@@ -9,8 +9,101 @@ module Dry
|
|
|
9
9
|
class Error < StandardError
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
# Raise from a command or callback to stop the CLI with a message for the user.
|
|
13
|
+
#
|
|
14
|
+
# The CLI prints the message (with no backtrace) to its stderr, then exits with {#exit_code}.
|
|
15
|
+
# Subclass this for your own user-facing errors.
|
|
16
|
+
#
|
|
17
|
+
# Use this for problems the user should know about and might be able to address, such as a
|
|
18
|
+
# missing file or a failed subprocess. Do not use it for bugs. Other errors are not rescued, so
|
|
19
|
+
# they keep their backtrace.
|
|
20
|
+
#
|
|
21
|
+
# This is a `StandardError`, so a broad `rescue` (such as `rescue => e`) between the raise and
|
|
22
|
+
# the CLI will catch it, and the CLI will not see it. To stop in a way that no `rescue` can catch,
|
|
23
|
+
# use `throw :exit` instead.
|
|
24
|
+
#
|
|
25
|
+
# @example
|
|
26
|
+
# raise Dry::CLI::ExitError, "config.yml not found"
|
|
27
|
+
# raise Dry::CLI::ExitError.new("deploy failed", exit_code: 2)
|
|
28
|
+
#
|
|
29
|
+
# @api public
|
|
30
|
+
# @since 2.0.0
|
|
31
|
+
class ExitError < Error
|
|
32
|
+
# @param message [String] the message to print to stderr
|
|
33
|
+
# @param exit_code [Integer] the code to exit with (defaults to 1)
|
|
34
|
+
#
|
|
35
|
+
# @api public
|
|
36
|
+
# @since 2.0.0
|
|
37
|
+
def initialize(message, exit_code: 1)
|
|
38
|
+
super(message)
|
|
39
|
+
@exit_code = exit_code
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Returns the code the CLI exits with.
|
|
43
|
+
#
|
|
44
|
+
# This is 1 when no code is set, such as when a subclass gives `exit_code: nil` or does not
|
|
45
|
+
# call `super` in its `#initialize`. This way, a missing code never makes an error exit with 0.
|
|
46
|
+
#
|
|
47
|
+
# @return [Integer]
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
# @since 2.0.0
|
|
51
|
+
def exit_code = @exit_code || 1
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# @api public
|
|
55
|
+
# @since x.y.z
|
|
56
|
+
class InvalidColorError < Error
|
|
57
|
+
attr_reader :value, :expected
|
|
58
|
+
|
|
59
|
+
def initialize(value:, expected:)
|
|
60
|
+
@value = value
|
|
61
|
+
@expected = expected
|
|
62
|
+
super()
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def message
|
|
66
|
+
"ERROR: invalid color #{@value.inspect}; expected #{@expected}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
12
70
|
# @since 1.4.0
|
|
13
71
|
class ValueError < Error
|
|
72
|
+
attr_reader :value, :argument
|
|
73
|
+
|
|
74
|
+
def initialize(value: nil, argument: nil)
|
|
75
|
+
@value = value
|
|
76
|
+
@argument = argument
|
|
77
|
+
super
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def message
|
|
81
|
+
if @value.nil? && @argument
|
|
82
|
+
"ERROR: \"#{@argument.name}\" is required"
|
|
83
|
+
elsif @argument
|
|
84
|
+
"ERROR: invalid argument \"#{@value}\" for \"#{@argument.name}\"; " \
|
|
85
|
+
"accepted values: #{@argument.values_description}"
|
|
86
|
+
else
|
|
87
|
+
"ERROR: invalid argument \"#{@value}\""
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# @since NEXT
|
|
93
|
+
class CastError < Error
|
|
94
|
+
def initialize(arg_name:, original_exception: nil)
|
|
95
|
+
super
|
|
96
|
+
@arg_name = arg_name
|
|
97
|
+
@original_exception = original_exception
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def message
|
|
101
|
+
msg = "ERROR when casting #{@arg_name}"
|
|
102
|
+
if @original_exception
|
|
103
|
+
msg += ": #{@original_exception.message}."
|
|
104
|
+
end
|
|
105
|
+
msg
|
|
106
|
+
end
|
|
14
107
|
end
|
|
15
108
|
|
|
16
109
|
# @since 0.2.1
|
|
@@ -22,6 +115,19 @@ module Dry
|
|
|
22
115
|
end
|
|
23
116
|
end
|
|
24
117
|
|
|
118
|
+
# Raised when a command is defined incorrectly, such as:
|
|
119
|
+
#
|
|
120
|
+
# - a required argument declared after an optional one
|
|
121
|
+
# - an argument declared after an array argument
|
|
122
|
+
# - an argument declared with both `required: true` and a `default:`
|
|
123
|
+
# - an option added to a command that already declares one with the same name, but with
|
|
124
|
+
# different settings
|
|
125
|
+
#
|
|
126
|
+
# @api public
|
|
127
|
+
# @since 2.0.0
|
|
128
|
+
class CommandDefinitionError < Error
|
|
129
|
+
end
|
|
130
|
+
|
|
25
131
|
# @since 0.2.0
|
|
26
132
|
class InvalidCallbackError < Error
|
|
27
133
|
# @since 0.2.0
|
data/lib/dry/cli/inline.rb
CHANGED
|
@@ -60,13 +60,13 @@ module Dry
|
|
|
60
60
|
# end
|
|
61
61
|
#
|
|
62
62
|
# @since 0.6.0
|
|
63
|
-
def run(arguments: ARGV,
|
|
63
|
+
def run(arguments: ARGV, stdout: $stdout)
|
|
64
64
|
command = AnonymousCommand
|
|
65
65
|
command.define_method(:call) do |**args|
|
|
66
66
|
yield(**args)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
Dry.CLI(command).call(arguments: arguments,
|
|
69
|
+
Dry.CLI(command).call(arguments: arguments, stdout: stdout)
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
72
|
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
class CLI
|
|
5
|
+
# Base class for namespaces
|
|
6
|
+
#
|
|
7
|
+
# @since 1.1.1
|
|
8
|
+
class Namespace
|
|
9
|
+
# @since 1.1.1
|
|
10
|
+
# @api private
|
|
11
|
+
def self.inherited(base)
|
|
12
|
+
super
|
|
13
|
+
base.class_eval do
|
|
14
|
+
@description = nil
|
|
15
|
+
@long_description = nil
|
|
16
|
+
@examples = []
|
|
17
|
+
@arguments = []
|
|
18
|
+
@options = []
|
|
19
|
+
@subcommands = []
|
|
20
|
+
end
|
|
21
|
+
base.extend ClassMethods
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @since 1.1.1
|
|
25
|
+
# @api private
|
|
26
|
+
module ClassMethods
|
|
27
|
+
# @since 1.1.1
|
|
28
|
+
# @api private
|
|
29
|
+
attr_reader :description
|
|
30
|
+
|
|
31
|
+
# @api private
|
|
32
|
+
attr_reader :long_description
|
|
33
|
+
|
|
34
|
+
# @since 1.1.1
|
|
35
|
+
# @api private
|
|
36
|
+
attr_reader :examples
|
|
37
|
+
|
|
38
|
+
# @since 1.1.1
|
|
39
|
+
# @api private
|
|
40
|
+
attr_reader :arguments
|
|
41
|
+
|
|
42
|
+
# @since 1.1.1
|
|
43
|
+
# @api private
|
|
44
|
+
attr_reader :options
|
|
45
|
+
|
|
46
|
+
# @since 1.1.1
|
|
47
|
+
# @api private
|
|
48
|
+
attr_accessor :subcommands
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Set the description of the namespace
|
|
52
|
+
#
|
|
53
|
+
# @param description [String] the description
|
|
54
|
+
#
|
|
55
|
+
# @example
|
|
56
|
+
# require "dry/cli"
|
|
57
|
+
#
|
|
58
|
+
# class YourNamespace < Dry::CLI::Namespace
|
|
59
|
+
# desc "Collection of really useful commands"
|
|
60
|
+
#
|
|
61
|
+
# class YourCommand < Dry::CLI::Command
|
|
62
|
+
# # ...
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# @api public
|
|
67
|
+
# @since 1.1.1
|
|
68
|
+
def self.desc(description)
|
|
69
|
+
@description = description
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Set the long description of the namespace
|
|
73
|
+
#
|
|
74
|
+
# It is printed in the "Description" section of the full namespace help (`--help`). Short help
|
|
75
|
+
# (`-h`) and command listings keep using the short description set via `.desc`. When no long
|
|
76
|
+
# description is set, `--help` falls back to the short description.
|
|
77
|
+
#
|
|
78
|
+
# @param long_description [String] the long description
|
|
79
|
+
#
|
|
80
|
+
# @example
|
|
81
|
+
# require "dry/cli"
|
|
82
|
+
#
|
|
83
|
+
# class YourNamespace < Dry::CLI::Namespace
|
|
84
|
+
# desc "Collection of really useful commands"
|
|
85
|
+
# long_desc <<~DESC
|
|
86
|
+
# A longer, multi-line explanation of what the commands in
|
|
87
|
+
# this namespace do and how they relate to each other.
|
|
88
|
+
# DESC
|
|
89
|
+
#
|
|
90
|
+
# class YourCommand < Dry::CLI::Command
|
|
91
|
+
# # ...
|
|
92
|
+
# end
|
|
93
|
+
# end
|
|
94
|
+
#
|
|
95
|
+
# @api public
|
|
96
|
+
# @since unreleased
|
|
97
|
+
def self.long_desc(long_description)
|
|
98
|
+
@long_description = long_description
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @since 1.1.1
|
|
102
|
+
# @api private
|
|
103
|
+
def self.default_params
|
|
104
|
+
{}
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @since 1.1.1
|
|
108
|
+
# @api private
|
|
109
|
+
def self.required_arguments
|
|
110
|
+
[]
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# @api private
|
|
114
|
+
def self.required_options
|
|
115
|
+
[]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# @since 1.1.1
|
|
119
|
+
# @api private
|
|
120
|
+
def self.subcommands
|
|
121
|
+
subcommands
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
data/lib/dry/cli/option.rb
CHANGED
|
@@ -4,96 +4,99 @@ module Dry
|
|
|
4
4
|
class CLI
|
|
5
5
|
# Command line option
|
|
6
6
|
#
|
|
7
|
-
# @since 0.1.0
|
|
8
7
|
# @api private
|
|
9
8
|
class Option
|
|
10
|
-
# @since 0.1.0
|
|
11
|
-
# @api private
|
|
12
9
|
attr_reader :name
|
|
13
10
|
|
|
14
|
-
# @since 0.1.0
|
|
15
|
-
# @api private
|
|
16
11
|
attr_reader :options
|
|
17
12
|
|
|
18
|
-
#
|
|
19
|
-
#
|
|
13
|
+
# The accepted values, as strings.
|
|
14
|
+
#
|
|
15
|
+
# Values are given on the command line as strings, so they're normalized rather than compared
|
|
16
|
+
# across types. `options[:values]` keeps whatever was declared.
|
|
17
|
+
attr_reader :values
|
|
18
|
+
|
|
20
19
|
def initialize(name, options = {})
|
|
21
20
|
@name = name
|
|
22
21
|
@options = options
|
|
22
|
+
@values = options[:values]&.map(&:to_s)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
# @since 0.1.0
|
|
26
|
-
# @api private
|
|
27
25
|
def aliases
|
|
28
26
|
options[:aliases] || []
|
|
29
27
|
end
|
|
30
28
|
|
|
31
|
-
# @since 0.1.0
|
|
32
|
-
# @api private
|
|
33
29
|
def desc
|
|
34
30
|
desc = options[:desc]
|
|
35
|
-
values ? "#{desc}: (#{
|
|
31
|
+
values ? "#{desc}: (#{values_description})" : desc
|
|
36
32
|
end
|
|
37
33
|
|
|
38
|
-
#
|
|
39
|
-
# @api private
|
|
34
|
+
# An option with a default always has a value, so the user never needs to give it.
|
|
40
35
|
def required?
|
|
41
|
-
options[:required]
|
|
36
|
+
options[:required] && default.nil?
|
|
42
37
|
end
|
|
43
38
|
|
|
44
|
-
# @since 0.1.0
|
|
45
|
-
# @api private
|
|
46
39
|
def type
|
|
47
40
|
options[:type]
|
|
48
41
|
end
|
|
49
42
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
def cast_callable
|
|
44
|
+
options[:cast]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Returns a human-readable list of this option's accepted `values`, e.g. "irb, pry, ripl".
|
|
48
|
+
def values_description
|
|
49
|
+
values&.join(", ")
|
|
54
50
|
end
|
|
55
51
|
|
|
56
|
-
# @since 0.1.0
|
|
57
|
-
# @api private
|
|
58
52
|
def boolean?
|
|
59
53
|
type == :boolean
|
|
60
54
|
end
|
|
61
55
|
|
|
62
|
-
# @api private
|
|
63
56
|
def flag?
|
|
64
57
|
type == :flag
|
|
65
58
|
end
|
|
66
59
|
|
|
67
|
-
# @since 0.3.0
|
|
68
|
-
# @api private
|
|
69
60
|
def array?
|
|
70
61
|
type == :array
|
|
71
62
|
end
|
|
72
63
|
|
|
73
|
-
# @since 0.1.0
|
|
74
|
-
# @api private
|
|
75
64
|
def default
|
|
76
65
|
options[:default]
|
|
77
66
|
end
|
|
78
67
|
|
|
79
|
-
# @since 0.1.0
|
|
80
|
-
# @api private
|
|
81
68
|
def description_name
|
|
82
69
|
options[:label] || name.upcase
|
|
83
70
|
end
|
|
84
71
|
|
|
85
|
-
# @since 0.1.0
|
|
86
|
-
# @api private
|
|
87
72
|
def argument?
|
|
88
73
|
false
|
|
89
74
|
end
|
|
90
75
|
|
|
91
|
-
#
|
|
92
|
-
#
|
|
76
|
+
# The subset of `#options` that must match for two declarations of the same option to be
|
|
77
|
+
# interchangeable, normalized for comparison.
|
|
93
78
|
#
|
|
79
|
+
# `:cast` is excluded because it's typically a proc or a Dry::Types object, neither of which
|
|
80
|
+
# compares meaningfully. `:desc`, `:label` and `:aliases` are excluded because they don't
|
|
81
|
+
# change how a value is parsed; the first declaration wins.
|
|
82
|
+
def compatibility_options
|
|
83
|
+
# Compare the declared :required rather than `#required?`, which is false whenever there's
|
|
84
|
+
# a default
|
|
85
|
+
{type: type, required: !!options[:required], values: values, default: default}
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The names of the `#compatibility_options` that stop this and `other` being interchangeable,
|
|
89
|
+
# if any.
|
|
90
|
+
def incompatible_options(other)
|
|
91
|
+
theirs = other.compatibility_options
|
|
92
|
+
|
|
93
|
+
compatibility_options.reject { |name, value| theirs[name] == value }.keys
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
|
94
97
|
def parser_options
|
|
95
98
|
dasherized_name = Inflector.dasherize(name)
|
|
96
|
-
parser_options
|
|
99
|
+
parser_options = []
|
|
97
100
|
|
|
98
101
|
if boolean?
|
|
99
102
|
parser_options << "--[no-]#{dasherized_name}"
|
|
@@ -104,15 +107,21 @@ module Dry
|
|
|
104
107
|
parser_options << "--#{dasherized_name} #{name}"
|
|
105
108
|
end
|
|
106
109
|
|
|
107
|
-
|
|
108
|
-
|
|
110
|
+
if array?
|
|
111
|
+
# Array options can't also give `values` to OptionParser. This would match against the
|
|
112
|
+
# values array and return a single string member, skipping the `Array` conversion itself.
|
|
113
|
+
# {Parser} validates their values instead.
|
|
114
|
+
parser_options << Array
|
|
115
|
+
elsif values
|
|
116
|
+
parser_options << values
|
|
117
|
+
end
|
|
118
|
+
|
|
109
119
|
parser_options.unshift(*alias_names) if aliases.any?
|
|
110
120
|
parser_options << desc if desc
|
|
111
121
|
parser_options
|
|
112
122
|
end
|
|
123
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
|
113
124
|
|
|
114
|
-
# @since 0.1.0
|
|
115
|
-
# @api private
|
|
116
125
|
def alias_names
|
|
117
126
|
aliases
|
|
118
127
|
.map { |name| name.gsub(/^-{1,2}/, "") }
|
|
@@ -122,26 +131,54 @@ module Dry
|
|
|
122
131
|
.map { |name| boolean? || flag? ? name : "#{name} VALUE" }
|
|
123
132
|
end
|
|
124
133
|
|
|
125
|
-
# @api private
|
|
126
134
|
def valid_value?(value)
|
|
127
|
-
|
|
128
|
-
return true if
|
|
135
|
+
return true if value.nil? && !required?
|
|
136
|
+
return true if values.nil?
|
|
137
|
+
|
|
138
|
+
if array?
|
|
139
|
+
value.all? { accepted_value?(_1) }
|
|
140
|
+
else
|
|
141
|
+
accepted_value?(value)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def cast(value)
|
|
146
|
+
return value unless cast_callable.respond_to?(:call)
|
|
129
147
|
|
|
130
148
|
if array?
|
|
131
|
-
|
|
149
|
+
value.map { cast_single(_1) }
|
|
132
150
|
else
|
|
133
|
-
|
|
151
|
+
cast_single(value)
|
|
134
152
|
end
|
|
135
153
|
end
|
|
154
|
+
|
|
155
|
+
private
|
|
156
|
+
|
|
157
|
+
def accepted_value?(value)
|
|
158
|
+
values.include?(value.to_s)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def cast_single(value)
|
|
162
|
+
cast_callable.call(value)
|
|
163
|
+
rescue StandardError => exception
|
|
164
|
+
raise CastError.new(arg_name: name, original_exception: exception)
|
|
165
|
+
end
|
|
136
166
|
end
|
|
137
167
|
|
|
138
168
|
# Command line argument
|
|
139
169
|
#
|
|
140
|
-
# @since 0.1.0
|
|
141
170
|
# @api private
|
|
142
171
|
class Argument < Option
|
|
143
|
-
|
|
144
|
-
|
|
172
|
+
def initialize(name, options = {})
|
|
173
|
+
super
|
|
174
|
+
|
|
175
|
+
# A default would make a required argument optional when it comes last, and would never be
|
|
176
|
+
# used anywhere else, since giving the arguments after it means giving this one too
|
|
177
|
+
if options[:required] && !default.nil?
|
|
178
|
+
raise CommandDefinitionError, "argument `#{name}' can't be both required and have a default"
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
145
182
|
def argument?
|
|
146
183
|
true
|
|
147
184
|
end
|