class Dry::CLI
General purpose Command
Line Interface (CLI
) framework for Ruby
@since 0.1.0
General purpose Command
Line Interface (CLI
) framework for Ruby
@since 0.1.0
Constants
- VERSION
@since 0.1.0
Attributes
@since 0.6.0 @api private
@since 0.6.0 @api private
@since 0.6.0 @api private
@since 0.6.0 @api private
Public Class Methods
Check if command
@param command [Object] the command to check
@return [TrueClass,FalseClass] true if instance of `Dry::CLI::Command`
@since 0.1.0 @api private
# File lib/dry/cli.rb, line 28 def self.command?(command) case command when Class command.ancestors.include?(Command) else command.is_a?(Command) end end
Create a new instance
@param command_or_registry [Dry::CLI::Registry, Dry::CLI::Command]
a registry or singular command
@param &block [Block] a configuration block for registry
@return [Dry::CLI] the new instance @since 0.1.0
# File lib/dry/cli.rb, line 45 def initialize(command_or_registry = nil, &block) @kommand = command_or_registry if command?(command_or_registry) @registry = if block_given? anonymous_registry(&block) else command_or_registry end end
Public Instance Methods
Invoke the CLI
@param arguments [Array<string>] the command line arguments (defaults to `ARGV`) @param out [IO] the standard output (defaults to `$stdout`) @param err [IO] the error output (defaults to `$stderr`)
@since 0.1.0
# File lib/dry/cli.rb, line 63 def call(arguments: ARGV, out: $stdout, err: $stderr) @out, @err = out, err kommand ? perform_command(arguments) : perform_registry(arguments) rescue SignalException => e signal_exception(e) rescue Errno::EPIPE # no op end
Private Instance Methods
Generates registry in runtime
@param &block [Block] configuration for the registry
@return [Module] module extended with registry abilities and configured with a block
@since 0.4.0 @api private
# File lib/dry/cli.rb, line 202 def anonymous_registry(&block) registry = Module.new { extend(Dry::CLI::Registry) } if block.arity.zero? registry.instance_eval(&block) else yield(registry) end registry end
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 146 def build_command(command) command.is_a?(Class) ? command.new : command end
Check if command
@param command [Object] the command to check
@return [TrueClass,FalseClass] true if instance of `Dry::CLI::Command`
@since 0.1.0 @api private
@see .command?
# File lib/dry/cli.rb, line 190 def command?(command) CLI.command?(command) end
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 159 def error(result) err.puts(result.error) exit(1) end
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 152 def help(command, prog_name) out.puts Banner.call(command, prog_name) exit(0) # Successful exit end
Parse arguments for a command.
It may exit in case of error, or in case of help.
@param result [Dry::CLI::CommandRegistry::LookupResult] @param out [IO] sta output
@return [Array<Dry:CLI::Command, Array>] returns an array where the
first element is a command and the second one is the list of arguments
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 132 def parse(command, arguments, names) prog_name = ProgramName.call(names) result = Parser.call(command, arguments, prog_name) return help(command, prog_name) if result.help? return error(result) if result.error? [build_command(command), result.arguments] end
Invoke the CLI
if singular command passed
@param arguments [Array<string>] the command line arguments @param out [IO] the standard output (defaults to `$stdout`)
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 97 def perform_command(arguments) command, args = parse(kommand, arguments, []) command.call(**args) end
Invoke the CLI
if registry passed
@param arguments [Array<string>] the command line arguments @param out [IO] the standard output (defaults to `$stdout`)
@since 0.6.0 @api private
# File lib/dry/cli.rb, line 109 def perform_registry(arguments) result = registry.get(arguments) return usage(result) unless result.found? command, args = parse(result.command, result.arguments, result.names) result.before_callbacks.run(command, args) command.call(**args) result.after_callbacks.run(command, args) end
Handles Exit codes for signals Fatal error signal ānā. Say 130 = 128 + 2 (SIGINT) or 137 = 128 + 9 (SIGKILL)
@since 0.7.0 @api private
# File lib/dry/cli.rb, line 176 def signal_exception(exception) exit(128 + exception.signo) end
@since 0.1.0 @api private
# File lib/dry/cli.rb, line 166 def usage(result) err.puts Usage.call(result) exit(1) end