dry-cli is a general-purpose framework for developing Command Line Interface (CLI) applications. It represents commands as objects that can be registered and offers support for arguments, options and forwarding variadic arguments to a sub-command.
Usage
The following is an elaborate example showcasing most of the available features.
Imagine you want to build a CLI executable foo for your Ruby project. The entire program is defined as below:
#!/usr/bin/env ruby
extend Dry::CLI::Registry
desc "Print version"
puts "1.0.0"
end
end
desc "Print input"
argument :input, desc: "Input to print"
example [
" # Prints 'wuh?'",
"hello, folks # Prints 'hello, folks'"
]
if input.nil?
puts "wuh?"
else
puts input
end
end
end
desc "Start Foo machinery"
argument :root, required: true, desc: "Root directory"
example [
"path/to/root # Start Foo at root directory"
]
puts "started - root: "
end
end
desc "Stop Foo machinery"
option :graceful, type: :boolean, default: true, desc: "Graceful stop"
puts "stopped - graceful: "
end
end
desc "Execute a task"
argument :task, type: :string, required: true, desc: "Task to be executed"
argument :dirs, type: :array, required: false, desc: "Optional directories"
puts "exec - task: , dirs: "
end
end
desc "Generate completion"
option :shell, default: "bash", values: %w[bash zsh]
puts "generated completion - shell: "
end
end
desc "Generate configuration"
option :apps, type: :array, default: [], aliases: ["-a"], desc: "Generate configuration for specific apps"
puts "generated configuration for apps: "
end
end
desc "Generate tests"
option :framework, default: "minitest", values: %w[minitest rspec]
puts "generated tests - framework: "
end
end
end
register "version", Version, aliases: ["v", "-v", "--version"]
register "echo", Echo
register "start", Start
register "stop", Stop
register "exec", Exec
register "completion", Completion, hidden: true
register "generate", aliases: ["g"] do
prefix.register "config", Generate::Configuration
prefix.register "test", Generate::Test
end
end
end
end
Dry::CLI.new(Foo::CLI::Commands).call
Available commands
With this code in place, we can now have a look at the command line usage by issuing the command foo without any arguments or options:
You can choose to hide a command from the help output by setting the hidden option to true when registering the command.
Help
It is also possible to get help for a particular command using the --help flag:
Optional arguments
A command can have optional arguments, which enables a default action in case nothing is provided:
Required arguments
On the other hand, required arguments will throw an error if not provided:
Array arguments
Captures all the remaining arguments in a single array.
Please note that array argument must be used as the last argument, as it works as a "catch-all".
Options
An option is a named argument that is passed after the command name and the arguments:
Boolean options
Boolean options are flags that change the behaviour of a command:
Array options
Array options are similar to arguments but must be named:
Subcommands
Subcommands are simply commands that have been registered under a nested path:
Aliases
Aliases are supported:
Subcommand aliases
Work similarly to command aliases