class Bovem::Command

This class represent a command (action) for Bovem.

Every command has the execution block and a set of option. Optionally, it also has before and after hooks.

@attribute name

@return [String] The name of this command. At runtime you can invoke it using the minimum number of letters to uniquely distinguish it from others.

@attribute description

@return [String] A very short description of what this command does.

@attribute banner

@return [String] A long description of this command.

@attribute synopsis

@return [String] A synopsis of the typical command line usage.

@attribute before

@return [Proc] A hook to execute before the command's action. It is executed only if no subcommand is executed.

@attribute action

@return [Proc] The action of this command. It is executed only if no subcommand is executed.

@attribute after

@return [Proc] A hook to execute after the command's action. It is executed only if no subcommand is executed.

@attribute application

@return [Application] The application this command belongs to.

@attribute parent

@return [Command] The parent of this command.

@attribute [r] commands

@return [Array] The subcommands associated to this command.

@attribute [r] options

@return [Array] The options available for this command.

@attribute [r] arguments

@return [Array] The arguments provided to this command.

@attribute [r] i18n

@return [I18n] A i18n helper.

Attributes

action[RW]
after[RW]
application[RW]
banner[RW]
before[RW]
description[RW]
i18n[R]
name[RW]
parent[RW]
synopsis[RW]

Public Class Methods

new(options = {}, &block) click to toggle source

Creates a new command.

@param options [Hash] The settings to initialize the command with.

# File lib/bovem/command.rb, line 327
def initialize(options = {}, &block)
  setup_with(options)
  instance_eval(&block) if block_given?
end

Public Instance Methods

application?() click to toggle source

Checks if the command is an application.

@return [Boolean] `true` if command is an application, `false` otherwise.

# File lib/bovem/command.rb, line 424
def application?
  is_a?(Bovem::Application)
end
banner?() click to toggle source

Check if this command has a banner.

@return [Boolean] `true` if this command has a banner, `false` otherwise.

description?() click to toggle source

Check if this command has a description.

@return [Boolean] `true` if this command has a description, `false` otherwise.

# File lib/bovem/command.rb, line 431
def description?
  description.present?
end
execute(args) click to toggle source

Executes this command, running its action or a subcommand.

@param args [Array] The arguments to pass to the command.

# File lib/bovem/command.rb, line 466
def execute(args)
  subcommand = Bovem::Parser.parse(self, args)

  if subcommand.present? # We have a subcommand to call
    commands[subcommand[:name]].execute(subcommand[:args])
  elsif action # Run our action
    # Run the before hook
    perform_action
  else # Show the help
    show_help
  end
end
full_name(suffix = nil, separator = ":") click to toggle source

Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix

@param suffix [String] A suffix to append. @param separator [String] The separator to use for components. @return [String] The full name.

# File lib/bovem/command.rb, line 346
def full_name(suffix = nil, separator = ":")
  return nil if application?
  [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
end
setup_with(options = {}) click to toggle source

Setups the command.

@param options [Hash] The settings for this command. @return [Command] The command.

# File lib/bovem/command.rb, line 446
def setup_with(options = {})
  options = {} unless options.is_a?(::Hash)
  setup_i18n(options)

  options.each_pair do |option, value|
    method = option.to_s

    if respond_to?(method) && self.method(method).arity != 0
      send(method, value)
    elsif respond_to?(method + "=")
      send(method + "=", value)
    end
  end

  self
end