Method: Amp::Command#initialize

Defined in:
lib/amp/commands/command.rb

#initialize(name, require_new = false) {|The| ... } ⇒ Command

Creates a command in the Amp system. Simply instantiating a new command will make it available to the Amp executable. You configure the command by filling out a block in the command’s initializer.

the command

Examples:

Command.new("add") do |c|
  c.opt :include, "Paths to include", 
            :options => {:short => "-I", :multi => true}
  c.opt :print_names, :desc => "Print the filenames", 
            :options => {:short => "-p", :default => false, 
                         :type => :boolean}
  c.on_run do |options, arguments|
    puts "silly!"
  end
end

Parameters:

  • name

    the name of the command that the user will use to call

  • &block

    a block of code where you can configure the command

Yields:

  • This block configures the command. Set up options, add an on_run handler, and so on.

Yield Parameters:

  • The

    command itself - it is yielded so you can modify it.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/amp/commands/command.rb', line 175

def initialize(name, require_new = false)
  # so that you can do additions to commands, just like ammending rake tasks
  full_name = (self.class.current_namespaces + [name]).join(":")
  name = full_name.to_sym
  if self.class.all_commands[name]
    yield self.class.all_commands[name] if block_given?
    return self.class.all_commands[name]
  end
  
  @name                = name
  @help                = ""
  @options             = []
  self.class.all_commands[name] = self
  @before = []
  @after  = []

  @workflows = []
  @synonyms  = []
  yield(self) if block_given?
  workflow :all if @workflows.empty?
  @options += GLOBAL_OPTIONS
end