Class: Excavator::Command
- Inherits:
-
Object
- Object
- Excavator::Command
- Defined in:
- lib/excavator/command.rb
Overview
Public: A Command is building block in Excavator. Commands are built incrementally (normally via methods in Excavator::DSL).
Instance Attribute Summary collapse
-
#block ⇒ Object
The logic for the command.
-
#desc ⇒ Object
Descriptors.
-
#name ⇒ Object
Descriptors.
-
#namespace ⇒ Object
Descriptors.
-
#param_definitions ⇒ Object
readonly
A list of Param objects.
-
#params ⇒ Object
readonly
Public: Parsed params (parsed using ParamParser).
-
#raw_params ⇒ Object
readonly
Public: An Array copy of arguments passed into this command (i.e, before ParamParser#parse! is called).
-
#runner ⇒ Object
readonly
Public: Reference to the Runner.
-
#unparsed_params ⇒ Object
readonly
Public: An Array of unparsed parameters.
Instance Method Summary collapse
-
#add_param(param) ⇒ Object
Public: Add a Param to this command.
-
#execute(*args) ⇒ Object
Public: Execute the Command’s block within a Excavator::Environment instance.
-
#execute_with_params(parsed_params = {}) ⇒ Object
Public: Execute this command.
-
#full_name ⇒ Object
Public: The full name of the Command.
-
#initialize(runner, options = {}) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(runner, options = {}) ⇒ Command
Returns a new instance of Command.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/excavator/command.rb', line 31 def initialize(runner, = {}) @runner = runner @name = [:name] @desc = [:desc] @block = [:block] @param_definitions = [:param_definitions] || [] @namespace = [:namespace] @param_parser = [:param_parser] || Excavator.param_parser_class.new @params = {} end |
Instance Attribute Details
#block ⇒ Object
The logic for the command
12 13 14 |
# File 'lib/excavator/command.rb', line 12 def block @block end |
#name ⇒ Object
Descriptors
9 10 11 |
# File 'lib/excavator/command.rb', line 9 def name @name end |
#namespace ⇒ Object
Descriptors
9 10 11 |
# File 'lib/excavator/command.rb', line 9 def namespace @namespace end |
#param_definitions ⇒ Object (readonly)
A list of Param objects
15 16 17 |
# File 'lib/excavator/command.rb', line 15 def param_definitions @param_definitions end |
#params ⇒ Object (readonly)
Public: Parsed params (parsed using ParamParser)
18 19 20 |
# File 'lib/excavator/command.rb', line 18 def params @params end |
#raw_params ⇒ Object (readonly)
Public: An Array copy of arguments passed into this command (i.e, before ParamParser#parse! is called)
22 23 24 |
# File 'lib/excavator/command.rb', line 22 def raw_params @raw_params end |
#runner ⇒ Object (readonly)
Public: Reference to the Runner
29 30 31 |
# File 'lib/excavator/command.rb', line 29 def runner @runner end |
#unparsed_params ⇒ Object (readonly)
Public: An Array of unparsed parameters. These are the left over arguments after ParamParser#parse! is called.
26 27 28 |
# File 'lib/excavator/command.rb', line 26 def unparsed_params @unparsed_params end |
Instance Method Details
#add_param(param) ⇒ Object
51 52 53 |
# File 'lib/excavator/command.rb', line 51 def add_param(param) self.param_definitions << param end |
#execute(*args) ⇒ Object
Public: Execute the Command’s block within a Excavator::Environment instance. Arguments are parsed, setup with default values, and checked to ensure the Command has all the proper parameters.
args - An Array of arguments to pass into the block. This is normally
the same format as ARGV.
Examples
command = Command.new( ... )
command.execute(["-a", "abc", "--foo", "bar"])
Returns the value returned by the Command’s block.
68 69 70 71 72 |
# File 'lib/excavator/command.rb', line 68 def execute(*args) args.flatten! parse_params args run end |
#execute_with_params(parsed_params = {}) ⇒ Object
Public: Execute this command. This is like #execute except the parameters is a Hash.
parsed_params - A Hash of params to pass into the Command’s block.
Examples
command = Command.new( ... )
command.execute({:a => "abc", :foo => "bar})
Returns the value returned by the Command’s block.
85 86 87 88 |
# File 'lib/excavator/command.rb', line 85 def execute_with_params(parsed_params = {}) parse_params [parsed_params] run end |