Module: Redmine::Command

Overview

The Command mixin helps define command objects used in the command line interface. Classes can mix in this behaviour to make it easier to take command line arguments, parse them and use the resulting options in the command execution.

Usage example:

class MyCommand
  extend Command

  usage 'greet [OPTIONS]' do |o|
    o.on '-n', '--name', 'Provide the name to be used' do |name|
      options[:name] = name
    end
  end

  def call(arguments)
    puts "Hello, #{options[:name]}!"
  end
end

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



25
26
27
# File 'lib/redmine/command.rb', line 25

def self.extended(base) # :nodoc:
  base.send(:prepend, InstanceMethods)
end

Instance Method Details

#usage(description, &block) ⇒ Object

Define the command line options available to this command. This is a wrapper around Ruby’s own OptionParser. Options defined here will be parsed out of incoming arguments, and what remains will be passed to #call. Within this block, you have access to an options hash that is also available in the #call method.



34
35
36
37
# File 'lib/redmine/command.rb', line 34

def usage(description, &block)
  @usage_description = description
  @usage_options = block
end

#usage_descriptionObject

Get the usage description set with #usage.



40
41
42
# File 'lib/redmine/command.rb', line 40

def usage_description
  @usage_description ||= ''
end

#usage_optionsObject

Get the OptionParser definition block set with #usage.



45
46
47
# File 'lib/redmine/command.rb', line 45

def usage_options
  @usage_options ||= ->(opts) {}
end