class Hanami::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

commands[R]

@since 0.1.0 @api private

Public Class Methods

command?(command) click to toggle source

Check if command

@param command [Object] the command to check

@return [TrueClass,FalseClass] true if instance of `Hanami::CLI::Command`

@since 0.1.0 @api private

# File lib/hanami/cli.rb, line 25
def self.command?(command)
  case command
  when Class
    command.ancestors.include?(Command)
  else
    command.is_a?(Command)
  end
end
const_missing(name) click to toggle source

@since 0.2.1

Calls superclass method
# File lib/hanami/cli/errors.rb, line 38
def self.const_missing(name)
  super unless name == :UnkwnownCommandError
  Hanami::Utils::Deprecation.new('UnkwnownCommandError is deprecated, please use UnknownCommandError')
  UnknownCommandError
end
new(registry) click to toggle source

Create a new instance

@param registry [Hanami::CLI::Registry] a registry

@return [Hanami::CLI] the new instance @since 0.1.0

# File lib/hanami/cli.rb, line 40
def initialize(registry)
  @commands = registry
end

Public Instance Methods

call(arguments: ARGV, out: $stdout) click to toggle source

Invoke the CLI

@param arguments [Array<string>] the command line arguments (defaults to `ARGV`) @param out [IO] the standard output (defaults to `$stdout`)

@since 0.1.0

# File lib/hanami/cli.rb, line 50
def call(arguments: ARGV, out: $stdout)
  result = commands.get(arguments)

  if result.found?
    command, args = parse(result, out)

    result.before_callbacks.run(command, args)
    command.call(args)
    result.after_callbacks.run(command, args)
  else
    usage(result, out)
  end
end

Private Instance Methods

command?(command) click to toggle source

Check if command

@param command [Object] the command to check

@return [TrueClass,FalseClass] true if instance of `Hanami::CLI::Command`

@since 0.1.0 @api private

@see .command?

# File lib/hanami/cli.rb, line 123
def command?(command)
  CLI.command?(command)
end
parse(result, out) click to toggle source

Parse arguments for a command.

It may exit in case of error, or in case of help.

@param result [Hanami::CLI::CommandRegistry::LookupResult] @param out [IO] sta output

@return [Array<Hanami:CLI::Command, Array>] returns an array where the

first element is a command and the second one is the list of arguments

@since 0.1.0 @api private

# File lib/hanami/cli.rb, line 82
def parse(result, out) # rubocop:disable Metrics/MethodLength
  command = result.command
  return [command, result.arguments] unless command?(command)

  result = Parser.call(command, result.arguments, result.names)

  if result.help?
    Banner.call(command, out)
    exit(0)
  end

  if result.error?
    out.puts(result.error)
    exit(1)
  end

  [command, result.arguments]
end
usage(result, out) click to toggle source

Prints the command usage and exit.

@param result [Hanami::CLI::CommandRegistry::LookupResult] @param out [IO] sta output

@since 0.1.0 @api private

# File lib/hanami/cli.rb, line 108
def usage(result, out)
  Usage.call(result, out)
  exit(1)
end