class Hanami::CLI::Command

Base class for commands

@since 0.1.0

Attributes

command_name[R]

@since 0.1.0 @api private

Public Class Methods

argument(name, options = {}) click to toggle source

Specify an argument

@param name [Symbol] the argument name @param options [Hash] a set of options

@since 0.1.0

@example Optional argument

require "hanami/cli"

class Hello < Hanami::CLI::Command
  argument :name

  def call(name: nil, **)
    if name.nil?
      puts "Hello, stranger"
    else
      puts "Hello, #{name}"
    end
  end
end

# $ foo hello
#   Hello, stranger

# $ foo hello Luca
#   Hello, Luca

@example Required argument

require "hanami/cli"

class Hello < Hanami::CLI::Command
  argument :name, required: true

  def call(name:, **)
    puts "Hello, #{name}"
  end
end

# $ foo hello Luca
#   Hello, Luca

# $ foo hello
#   ERROR: "foo hello" was called with no arguments
#   Usage: "foo hello NAME"

@example Multiple arguments

require "hanami/cli"

module Generate
  class Action < Hanami::CLI::Command
    argument :app,    required: true
    argument :action, required: true

    def call(app:, action:, **)
      puts "Generating action: #{action} for app: #{app}"
    end
  end
end

# $ foo generate action web home
#   Generating action: home for app: web

# $ foo generate action
#   ERROR: "foo generate action" was called with no arguments
#   Usage: "foo generate action APP ACTION"

@example Description

require "hanami/cli"

class Hello < Hanami::CLI::Command
  argument :name, desc: "The name of the person to greet"

  def call(name: nil, **)
    # ...
  end
end

# $ foo hello --help
#   Command:
#     foo hello
#
#   Usage:
#     foo hello [NAME]
#
#   Arguments:
#     NAME                # The name of the person to greet
#
#   Options:
#     --help, -h          # Print this help
# File lib/hanami/cli/command.rb, line 197
def self.argument(name, options = {})
  @arguments << Argument.new(name, options)
end
default_params() click to toggle source

@since 0.1.0 @api private

# File lib/hanami/cli/command.rb, line 323
def self.default_params
  params.each_with_object({}) do |param, result|
    result[param.name] = param.default unless param.default.nil?
  end
end
desc(description) click to toggle source

Set the description of the command

@param description [String] the description

@since 0.1.0

@example

require "hanami/cli"

class Echo < Hanami::CLI::Command
  desc "Prints given input"

  def call(*)
    # ...
  end
end
# File lib/hanami/cli/command.rb, line 67
def self.desc(description)
  @description = description
end
example(*examples) click to toggle source

Describe the usage of the command

@param examples [Array<String>] one or more examples

@since 0.1.0

@example

require "hanami/cli"

class Server < Hanami::CLI::Command
  example [
    "                    # Basic usage (it uses the bundled server engine)",
    "--server=webrick    # Force `webrick` server engine",
    "--host=0.0.0.0      # Bind to a host",
    "--port=2306         # Bind to a port",
    "--no-code-reloading # Disable code reloading"
  ]

  def call(*)
    # ...
  end
end

# $ foo server --help
#   # ...
#
#   Examples:
#     foo server                     # Basic usage (it uses the bundled server engine)
#     foo server --server=webrick    # Force `webrick` server engine
#     foo server --host=0.0.0.0      # Bind to a host
#     foo server --port=2306         # Bind to a port
#     foo server --no-code-reloading # Disable code reloading
# File lib/hanami/cli/command.rb, line 103
def self.example(*examples)
  @examples += examples.flatten
end
inherited(base) click to toggle source

@since 0.1.0 @api private

Calls superclass method
# File lib/hanami/cli/command.rb, line 13
def self.inherited(base)
  super
  base.extend ClassMethods
end
new(command_name:, **) click to toggle source

@since 0.1.0 @api private

# File lib/hanami/cli/command.rb, line 360
def initialize(command_name:, **)
  @command_name = command_name
end
option(name, options = {}) click to toggle source

Command line option (aka optional argument)

@param name [Symbol] the param name @param options [Hash] a set of options

@since 0.1.0

@example Basic usage

require "hanami/cli"

class Console < Hanami::CLI::Command
  param :engine

  def call(engine: nil, **)
    puts "starting console (engine: #{engine || :irb})"
  end
end

# $ foo console
# starting console (engine: irb)

# $ foo console --engine=pry
# starting console (engine: pry)

@example List values

require "hanami/cli"

class Console < Hanami::CLI::Command
  param :engine, values: %w(irb pry ripl)

  def call(engine: nil, **)
    puts "starting console (engine: #{engine || :irb})"
  end
end

# $ foo console
# starting console (engine: irb)

# $ foo console --engine=pry
# starting console (engine: pry)

# $ foo console --engine=foo
# Error: Invalid param provided

@example Description

require "hanami/cli"

class Console < Hanami::CLI::Command
  param :engine, desc: "Force a console engine"

  def call(engine: nil, **)
    # ...
  end
end

# $ foo console --help
# # ...
#
# Options:
#   --engine=VALUE                  # Force a console engine: (irb/pry/ripl)
#   --help, -h                      # Print this help

@example Boolean

require "hanami/cli"

class Server < Hanami::CLI::Command
  param :code_reloading, type: :boolean, default: true

  def call(code_reloading:, **)
    puts "staring server (code reloading: #{code_reloading})"
  end
end

# $ foo server
# starting server (code reloading: true)

# $ foo server --no-code-reloading
# starting server (code reloading: false)

# $ foo server --help
# # ...
#
# Options:
#   --[no]-code-reloading

@example Aliases

require "hanami/cli"

class Server < Hanami::CLI::Command
  param :port, aliases: ["-p"]

  def call(options)
    puts "staring server (port: #{options.fetch(:port, 2300)})"
  end
end

# $ foo server
# starting server (port: 2300)

# $ foo server --port=2306
# starting server (port: 2306)

# $ foo server -p 2306
# starting server (port: 2306)

# $ foo server --help
# # ...
#
# Options:
#   --port=VALUE, -p VALUE
# File lib/hanami/cli/command.rb, line 311
def self.option(name, options = {})
  @options << Option.new(name, options)
end
optional_arguments() click to toggle source

@since 0.1.0 @api private

# File lib/hanami/cli/command.rb, line 337
def self.optional_arguments
  arguments.reject(&:required?)
end
params() click to toggle source

@since 0.1.0 @api private

# File lib/hanami/cli/command.rb, line 317
def self.params
  (@arguments + @options).uniq
end
required_arguments() click to toggle source

@since 0.1.0 @api private

# File lib/hanami/cli/command.rb, line 331
def self.required_arguments
  arguments.select(&:required?)
end