Repository navigation
v2.0.0.rc1
Pre-releaseAdded
-
Support for command namespaces. (@gustavothecoder in #135)
Subclass
Dry::CLI::Namespaceto group commands under a name that is not a command itself:class DB < Dry::CLI::Namespace desc "Manage the database" end register "db", DB register "db migrate", DB::Migrate register "db rollback", DB::Rollback
Running
my-cli dblists the commands in the namespace, andmy-cli db --helpshows its help. -
A
long_descmethod for a long command descriptions. This will show when--helpis given, whereas-hwill show the short description. When no long description is provided, both--helpand-hshow the short description. (@aaronmallen in #160)class Echo < Dry::CLI::Command desc "Prints given input" long_desc <<~DESC Prints the given input back to standard output. When no input is given, it prints an empty line. DESC end
Dry::CLI::Namespacealso haslong_desc. -
A
:castoption for options and arguments, allowing to leverage Dry Types or simple procs/lambdas to cast values from string to some other type of value. (@katafrakt in #157, @timriley in #162)Give
cast:anything that responds to#call:Types = Dry.Types() class Info < Dry::CLI::Command argument :lines, cast: Types::Coercible::Integer, default: 10 option :sudo, cast: Types::Params::Bool, default: false option :uid, cast: ->(v) { Integer(v) } end
my-cli info 15 --sudo true --uid 16 # lines: 15, sudo: true, uid: 16With
type: :array, each value is cast on its own. If casting raises an error, the command does not run, and the error is printed:my-cli info --sudo maybe ERROR when casting sudo: maybe cannot be coerced to false. -
Array options can be supplied by repeated flags. (@Drowze in #159)
The following are now equivalent:
my-cli --array-flag foo,bar my-cli --array-flag foo --array-flag bar -
Dry::CLI::Command#auto_initialize, for taking select keywords that.newassigns itself, rather than passing on to#initialize. (@timriley in #167)It is common for a CLI app to set up "standard" dependencies that can be provided to every command class. Making these auto-initialized keywords keeps each command's
#initializefocused on its own distinct dependencies, while the standard dependencies are still assigned as expected. Command class authors do not need to supply**kwargsas an#initializeparameter and then callsuper(**kwargs), which reduces boilerplate, as well as the chance of bugs from these lines being forgotten.A subclass adding its own needs only to declare and assign them. Write it alongside your
#initialize, since the two share the job of setting the command up:# In a CLI app's base command class # `inflector:` becomes a standard dependency, and can also take a default value. private def auto_initialize(inflector: Dry::Inflector.new, **kwargs) super(**kwargs) @inflector = inflector end
This makes
inflector:work as an argument to.new, and assigns its value to an ivar automatically, before#initializeis called.Auto-initialized keywords are best used sparingly. Consider these for your CLI app's base command class only. A command cannot use auto-initialized keywords as their own
#initializeparameters, since it will never receive them. -
Dry::CLI::Command#invoke, for running one command from within another. (@parndt in #183)The other command is built with the auto-initialized keywords of the command invoking it, so it writes to the same streams and shares the same standard dependencies. Its option defaults apply to any options not given (or given as nil), and it is given only the keywords its
#callaccepts, so a command can pass along all of its own options:class Migrate < MyApp::Command def call(...) invoke(Create, ...) # ... end end
When a command passes along its options like this, its option defaults are passed along too, and replace the other command's defaults for any options they share.
Unlike running the other command from the command line, the given values are not cast to their option types or checked against
required:orvalues:, and nobeforeoraftercallbacks registered for the other command are run.Each auto-initialized keyword is read back through the reader of the same name, so every auto-initialized keyword needs a reader.
-
Registry#command_classandRegistry#option, for extending a command that another gem owns. (@afomera in #165)A third-party gem can now contribute the option its own hooks need, instead of that option having to be declared upfront by the gem that owns the command:
Hanami::CLI.after "generate action", Commands::Generate::Action Hanami::CLI.option "generate action", :skip_tests, type: :flag, default: false, desc: "Skip test generation"
Registry#command_classreturns the registered command's class, as an escape hatch for anything else its class-level DSL offers.Adding the same option twice is allowed, so independent gems can each contribute the option they need without coordinating, as long as they agree on
:type,:required,:valuesand:default. OtherwiseDry::CLI::CommandDefinitionErroris raised.:castis not compared, since procs aren't meaningfully comparable. -
Dry::CLI.accepted_kwargs, which returns only the keyword args that a method or proc accepts, based on its params. Use it when you wrap a command's#call, to pass on only the keyword args that the next#callaccepts. (@timriley in #181)class Deploy < Dry::CLI::Command def call(target:, force: false) = ... end kwargs = {target: "production", env: "test"} Dry::CLI.accepted_kwargs(Deploy.new.method(:call), kwargs) # => {target: "production"}
-
Commands and callbacks can stop the CLI with an exit code by throwing
:exit. The CLI catches the throw and exits with the code. A barethrow :exitexits with 0. (@timriley in #180)class Deploy < Dry::CLI::Command def call(**) unless File.exist?("config.yml") stderr.puts "config.yml not found" throw :exit, 1 end # ... end end
-
Dry::CLI::ExitError, for errors the user is meant to see. Raise it (or your own subclass) from anywhere in a command or callback. The CLI prints its message to stderr, with no backtrace, then exits with itsexit_code(1 by default). Other errors are not rescued, so they keep their backtrace. (@timriley in #180)raise Dry::CLI::ExitError, "config.yml not found" raise Dry::CLI::ExitError.new("deploy failed", exit_code: 2)
-
Dry::CLI#callaccepts anexit:argument, which the CLI calls with the exit code instead ofKernel#exit. With this, tests can check exit codes without stubbingKernel#exitor rescuingSystemExit. (@timriley in #180)exit_code = nil cli.call(arguments: ["deploy"], exit: ->(code) { exit_code = code }) exit_code # => 1
Existing commands that call
Kernel#exititself still exit the process directly, and do not useexit:. To useexit:, change these calls tothrow :exit, code, or raiseDry::CLI::ExitError. -
Support for styled text. (@aaronmallen and @timriley in #166)
Use
stylein your commands (orDry::CLI::Style) directly to build styles, then send text to those styles. Once you pass that text to an output stream, it will be styled (and its colors gracefully degraded if required) based on the terminal's capabilities. Styled text sent to a non-terminal stream (such as when a CLI command has output redirected to a file) will not be styled.# Dry::CLI::Command gets `style` at both the class and instance level; use Dry::CLI::StyleMixin to add this to your own classes. ERROR = style.bold.red def call text = ERROR["boom"] stderr.puts text # renders as "\e[1;31mboom\e[0m" end
Command#stdoutand#stderrare now always instances ofDry::CLI::Stream. Usestdout.rawto access the underlying IO object directly. -
Formatting support for styled text. (@alassek in #168)
Dry::CLI::Style::Textobjects have a#%method that behaves likeString#%but respects the style information.style.bold.red["%s"] % "boom" # => "\e[1;31mboom\e[0m"
-
Dry::CLI::Spinner: a simple, animated spinner for long-running tasks. (@alassek in #169)Four default styles: Dot, Ellipsis, Line, MiniDot.
The simplest case is a block to run the task and a static message to display (supports styled text):
Dry::CLI::Spinner::Dot.run("%{spinner} working...") do sleep 3 end
If you require more control, you can provide a block argument and register
before_tickandafter_tickcallbacks:template = style.bold.green["%{spinner}"] template += style.italic.dim[" waiting "] template += style.bold.white["%{num}"] template += style.italic.dim[" seconds"] Dry::CLI::Spinner::MiniDot.run(template) do |s| trailer = Dry::CLI::Spinner::Ellipsis.frames.cycle start = Time.now s.before_tick do |_stream, data| data[:num] = (Time.now - start).ceil end s.after_tick do |stream| stream << style.italic.dim[trailer.next] end s.run { sleep 6 } end
Changed
-
[BREAKING]
required: trueon options is enforced. (@capripot and @timriley in #122)Before,
required: truewas accepted on options but did nothing. Now, if a required option is not given, the command does not run, and prints an error that names the missing option. Required options are marked as REQUIRED in the help output. A required option with adefault:always has a value, so it is not enforced and not marked as required.If your commands already declare options with
required: trueand no default, they will now fail when the user leaves those options out.class Server < Dry::CLI::Command option :host, required: true, desc: "Host" option :port, required: true, desc: "Port" end
$ my-cli server --host=localhost ERROR: "my-cli server" is missing required option --port Usage: "my-cli server --host=VALUE --port=VALUE" $ my-cli server --help ... Options: --host=VALUE # REQUIRED Host --port=VALUE # REQUIRED Port --help, -h # Print this help -
[BREAKING] Declaring an argument with both
required: trueand adefault:raisesDry::CLI::CommandDefinitionError. Remove eitherrequired: trueor the default. (@timriley in #177) -
Declaring a required argument after an optional one, or any argument after an array argument, raises
Dry::CLI::CommandDefinitionError. Since arguments are matched by position, such a command could never be called correctly: a required argument could not be given without also giving the optional one before it, and an array argument takes all the remaining values, leaving none for the arguments after it. (@timriley in #177)argument :env argument :name, required: true # => required argument `name' can't be declared after optional argument `env' argument :files, type: :array argument :dest # => argument `dest' can't be declared after array argument `files'
-
The
exampleDSL now takes the example and its description as separate arguments, called once per example. Previously, examples were passed as a single array of strings with the description embedded after a#. (@aaronmallen and @timriley in #152)class Server < Dry::CLI::Command # Now: example "--server=webrick", "Force `webrick` server engine" example "--host=0.0.0.0", "Bind to a host" example "--port=2306", "Bind to a port" example "--no-code-reloading", "Disable code reloading" # Before: example [ "--server=webrick # Force `webrick` server engine", "--host=0.0.0.0 # Bind to a host", # ... ] end
-
Dry::CLI::Command.newnow takes three keyword arguments:stdout:,stdin:andstderr:. These can be used to inject I/O streams, which is useful for testing. (@aaronmallen in #151, @timriley in #167)These arguments are taken by
.newand set on the command instance before#initializeruns, so#initializein a subclass only needs to worry about its own arguments, with no call tosuperrequired:class Generate < Dry::CLI::Command def initialize(generator: Generator.new) @generator = generator end end Generate.new(stdout: io, generator: generator)
A command registered as an instance will have its streams replaced after it is built, so avoid building on top of the streams from inside
#initialize. Instead, build from a stream lazily, only when you use it. SeeDry::CLI::Commandfor details. -
Commands and callbacks are now passed only the keyword args that match the params their
#calldeclares, so they no longer need a**catch-all param to tolerate options contributed by other gems. (@afomera in #165)Every keyword arg is passed when
#callcan take them as a whole: when it declares a keyword splat param or a positional param. Otherwise they're matched against the keyword params it declares.For example, if another gem adds a
--skip-testsoption to this command, the command still works:class Generate < Dry::CLI::Command argument :name, required: true # Before, this raised ArgumentError (unknown keyword: :skip_tests) # unless it was `def call(name:, **)` def call(name:) # ... end end
-
Exits for help (status 0), invalid options and arguments (1), and unknown commands (1) now go through the
exit:given toDry::CLI#call. (@timriley in #180) -
Error messages for invalid input now name the option or argument that is wrong, instead of repeating everything that was given. (@katafrakt in #158)
$ my-cli server --unknown 1234 # Before ERROR: "my-cli server" was called with arguments "--unknown 1234" # Now ERROR: "my-cli server" was called with invalid option "--unknown" $ my-cli db rollback 4 # Before ERROR: "my-cli db rollback" was called with arguments "4" # Now ERROR: invalid argument "4" for "steps"; accepted values: 1, 2, 3If you match on error text (in tests, for example), you will need to update it.
-
Accepted values are now listed the same way in help output as in error messages, separated by commas rather than slashes. (@timriley in #163)
# Before --engine=VALUE # Engine: (irb/pry) # Now --engine=VALUE # Engine: (irb, pry)
Fixed
-
An
argumentwithtype: :arrayis now always given to the command as an array. When the command declared more arguments than were given, it previously received no value at all. (@timriley in #162)argument :task argument :files, type: :array
cmd # was {}, now {files: []} -
A required
argumentwithtype: :arrayno longer has its values repeated inargs. It takes all the remaining values, but all except the first were also given inargs. (@parndt in #178)argument :task, required: true argument :dirs, type: :array, required: true
cmd test lib spec # was dirs: ["lib", "spec"], args: ["spec"], now dirs: ["lib", "spec"] -
Redeclaring an argument, such as in a subclass to change its
desc, now keeps the argument in its original position. Previously it was moved to the end, so values given on the command line were matched to the wrong arguments. (@timriley in #177) -
Show array arguments last in the usage line of help output. (@gustavothecoder in #145)
-
An optional argument with
values:can now be left out. Before, leaving it out gave an error, as if it were required. Options already worked this way. (@svoop in #155)argument :mode, required: true, values: %w[fast slow] argument :level, values: %w[low high]
cmd fast # was an error, now {mode: "fast"} cmd fast high # {mode: "fast", level: "high"} cmd fast max # error: "max" is not an accepted value -
Options combining
type: :arraywithvalues:no longer return a single string instead of an array, and now accept comma-separated values. (@timriley in #161)option :require, type: :array, values: %w[foo bar baz]
cmd --require=foo # was "foo", now ["foo"] cmd --require=foo,bar # was an error, now ["foo", "bar"] -
Allow options and arguments to specify non-string
values:, such as integers. These are cast to strings when declared, then compared against the strings given on the command line. (@timriley in #161)option :level, values: [1, 2, 3]
cmd --level=1 # was an error, now "1"