Class: Rootage::Command

Inherits:
Scenario show all
Defined in:
lib/rootage/command.rb

Overview

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

Direct Known Subclasses

StandardCommand

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Scenario

#args, #current_phase, #exit_status, #info, #model, #running_thread

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Scenario

#abort, define_action, make, require, run, scenario_name

Methods included from ScenarioInterface

#define, #define_phase, #desc, #phase, #phase_class, #process_context_class

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



101
102
103
104
105
106
107
# 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

Class Attribute Details

.argument_definitionObject

Returns the value of attribute argument_definition.



32
33
34
# File 'lib/rootage/command.rb', line 32

def argument_definition
  @argument_definition
end

.option_definitionObject

Returns the value of attribute option_definition.



33
34
35
# File 'lib/rootage/command.rb', line 33

def option_definition
  @option_definition
end

.subcommandObject

Returns the value of attribute subcommand.



31
32
33
# File 'lib/rootage/command.rb', line 31

def subcommand
  @subcommand
end

Instance Attribute Details

#argument_definitionObject (readonly)

Returns the value of attribute argument_definition.



99
100
101
# File 'lib/rootage/command.rb', line 99

def argument_definition
  @argument_definition
end

#argvObject (readonly)

Returns the value of attribute argv.



97
98
99
# File 'lib/rootage/command.rb', line 97

def argv
  @argv
end

#option_definitionObject (readonly)

Returns the value of attribute option_definition.



98
99
100
# File 'lib/rootage/command.rb', line 98

def option_definition
  @option_definition
end

Class Method Details

.define_subcommand(name, subcommand) ⇒ Object

Define a subcommand.

Parameters:

  • name (String)

    subcommand name

  • subcommand (Class)

    subcommand class



74
75
76
# File 'lib/rootage/command.rb', line 74

def define_subcommand(name, subcommand)
  @subcommand[name] = subcommand
end

.has_subcommands?Boolean

Return true if the command has subcommands.

Returns:

  • (Boolean)

    true if the command has subcommands



82
83
84
# File 'lib/rootage/command.rb', line 82

def has_subcommands?
  not(@subcommand.values.compact.empty?)
end

.inherited(subclass) ⇒ Object



35
36
37
38
39
40
41
# 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) ⇒ Object

Define an action to phase "init".



56
57
58
# File 'lib/rootage/command.rb', line 56

def init(action, &b)
  define_action(:init, action, &b)
end

.toplevel?Boolean

Return true if the command is on toplevel.

Returns:

  • (Boolean)

    true if the command is on toplevel



64
65
66
# File 'lib/rootage/command.rb', line 64

def toplevel?
  @info.has_key?(:toplevel) ? @info[:toplevel] : false
end

Instance Method Details

#<<(object) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# 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

#exitObject

Exit running command and return the status.



142
143
144
145
# File 'lib/rootage/command.rb', line 142

def exit
  super
  Kernel.exit(exit_status)
end

#program_nameObject



147
148
149
# File 'lib/rootage/command.rb', line 147

def program_name
  scenario_name
end

#runvoid

This method returns an undefined value.

Run a lifecycle of the command.



132
133
134
135
136
137
138
139
# File 'lib/rootage/command.rb', line 132

def run
  load_requirements
  if has_subcommands?
    execute_subcommand
  end
  execute_phases
  exit
end

#scenario_nameObject Also known as: name



109
110
111
112
113
114
115
# File 'lib/rootage/command.rb', line 109

def scenario_name
  if @parent_name
    "%s %s" % [@parent_name, super]
  else
    super
  end
end