Parent

Clamp::Command

{Command} models a shell command. Each command invocation is a new object. Command options and parameters are represented as attributes (see {Command::Declaration}).

The main entry-point is {run}, which uses {parse} to populate attributes based on an array of command-line arguments, then calls {execute} (which you provide) to make it go.

Attributes

context[RW]
invocation_path[R]

@return [String] the path used to invoke this command

Public Class Methods

new(invocation_path, context = {}, parent_attribute_values = {}) click to toggle source

Create a command execution.

@param [String] invocation_path the path used to invoke the command @param [Hash] context additional data the command may need

# File lib/clamp/command.rb, line 27
def initialize(invocation_path, context = {}, parent_attribute_values = {})
  @invocation_path = invocation_path
  @context = context
  parent_attribute_values.each do |attribute, value|
    attribute.of(self).set(value)
  end
end

Public Instance Methods

execute() click to toggle source

Execute the command (assuming that all options/parameters have been set).

This method is designed to be overridden in sub-classes.

# File lib/clamp/command.rb, line 74
def execute
  raise "you need to define #execute"
end
help() click to toggle source

@return [String] usage documentation for this command

# File lib/clamp/command.rb, line 80
def help
  self.class.help(invocation_path)
end
parse(arguments) click to toggle source

Parse command-line arguments.

@param [Array<String>] arguments command-line arguments @return [Array<String>] unconsumed arguments

# File lib/clamp/command.rb, line 50
def parse(arguments)
  @remaining_arguments = arguments.dup
  parse_options
  parse_parameters
  parse_subcommand
  handle_remaining_arguments
end
remaining_arguments() click to toggle source

@return [Array<String>] unconsumed command-line arguments

# File lib/clamp/command.rb, line 41
def remaining_arguments
  @remaining_arguments
end
run(arguments) click to toggle source

Run the command, with the specified arguments.

This calls {parse} to process the command-line arguments, then delegates to {execute}.

@param [Array<String>] arguments command-line arguments

# File lib/clamp/command.rb, line 65
def run(arguments)
  parse(arguments)
  execute
end

Protected Instance Methods

handle_remaining_arguments() click to toggle source
# File lib/clamp/command.rb, line 92
def handle_remaining_arguments
  unless remaining_arguments.empty?
    signal_usage_error "too many arguments"
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.