Class: Hanami::CLI::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
gems/gems/hanami-cli-0.3.0/lib/hanami/cli/command.rb

Overview

Base class for commands

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.argument(name, options = {}) ⇒ Object

Specify an argument

Examples:

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

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"

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"

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

Parameters:

  • name (Symbol)

    the argument name

  • options (Hash) (defaults to: {})

    a set of options

Since:

  • 0.1.0

def self.argument(name, options = {})
  @arguments << Argument.new(name, options)
end

.desc(description) ⇒ Object

Set the description of the command

Examples:

require "hanami/cli"

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

  def call(*)
    # ...
  end
end

Parameters:

  • description (String)

    the description

Since:

  • 0.1.0

def self.desc(description)
  @description = description
end

.example(*examples) ⇒ Object

Describe the usage of the command

Examples:

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

Parameters:

  • examples (Array<String>)

    one or more examples

Since:

  • 0.1.0

def self.example(*examples)
  @examples += examples.flatten
end

.option(name, options = {}) ⇒ Object

Command line option (aka optional argument)

Examples:

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)

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

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

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

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

Parameters:

  • name (Symbol)

    the param name

  • options (Hash) (defaults to: {})

    a set of options

Since:

  • 0.1.0

def self.option(name, options = {})
  @options << Option.new(name, options)
end