class Rootage::Command

Command is a scenario that has subcommands, options and arguments.

Attributes

argument_definition[RW]
option_definition[RW]
subcommand[RW]
argument_definition[R]
argv[R]
option_definition[R]

Public Class Methods

define_subcommand(name, subcommand) click to toggle source

Define a subcommand.

@param name [String]

subcommand name

@param subcommand [Class]

subcommand class
# File lib/rootage/command.rb, line 74
def define_subcommand(name, subcommand)
  @subcommand[name] = subcommand
end
has_subcommands?() click to toggle source

Return true if the command has subcommands.

@return [Boolean]

true if the command has subcommands
# File lib/rootage/command.rb, line 82
def has_subcommands?
  not(@subcommand.values.compact.empty?)
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/rootage/command.rb, line 35
def inherited(subclass)
  super

  subclass.subcommand = @subcommand.clone
  subclass.argument_definition = @argument_definition.copy
  subclass.option_definition = @option_definition.copy
end
init(action, &b) click to toggle source

Define an action to phase “init”.

# File lib/rootage/command.rb, line 56
def init(action, &b)
  define_action(:init, action, &b)
end
new(*args) click to toggle source
Calls superclass method
# File lib/rootage/command.rb, line 101
def initialize(*args)
  super
  @argv = args[0].clone
  @parent_name = args[1]
  @argument_definition = self.class.argument_definition.copy
  @option_definition = self.class.option_definition.copy
end
toplevel?() click to toggle source

Return true if the command is on toplevel.

@return [Boolean]

true if the command is on toplevel
# File lib/rootage/command.rb, line 64
def toplevel?
  @info.has_key?(:toplevel) ? @info[:toplevel] : false
end

Public Instance Methods

<<(object) click to toggle source
Calls superclass method
# File lib/rootage/command.rb, line 118
def <<(object)
  case object
  when Argument
    argument_definition << object
  when Option
    option_definition << object
  else
    super
  end
end
exit() click to toggle source

Exit running command and return the status.

Calls superclass method
# File lib/rootage/command.rb, line 142
def exit
  super
  Kernel.exit(exit_status)
end
name()
Alias for: scenario_name
program_name() click to toggle source
# File lib/rootage/command.rb, line 147
def program_name
  scenario_name
end
run() click to toggle source

Run a lifecycle of the command.

@return [void]

# File lib/rootage/command.rb, line 132
def run
  load_requirements
  if has_subcommands?
    execute_subcommand
  end
  execute_phases
  exit
end
scenario_name() click to toggle source
Calls superclass method
# File lib/rootage/command.rb, line 109
def scenario_name
  if @parent_name
    "%s %s" % [@parent_name, super]
  else
    super
  end
end
Also aliased as: name

Private Instance Methods

enter_phase(phase_name) click to toggle source
Calls superclass method
# File lib/rootage/command.rb, line 153
def enter_phase(phase_name)
  Timeout.timeout(phase(phase_name).config[:timeout]) do
    super
  end
rescue PhaseTimeoutError => e
  abort(e)
end
execute_subcommand() click to toggle source

Execute subcommand.

# File lib/rootage/command.rb, line 162
def execute_subcommand
  if subcommand_name = argv.first
    if subcommand.has_key?(subcommand_name)
      # run the subcommand
      return subcommand[subcommand_name].run(argv.drop(1), scenario_name)
    else
      unless subcommand_name[0] == "-"
        abort("There is no such subcommand: %{name}" % {name: subcommand_name})
      end
    end
  else
    abort('"%{name}" requires a subcommand name.' % {name: scenario_name})
  end
end