class WhirledPeas::Command::Base
Abstract base class for all commands
Attributes
Public Class Methods
Returns the name of the command as expected by the command line script. By convention, this name is snake case version of the class name, e.g. the command `do_something` would be implemented by `WhirledPeas::Command::DoSomething`, which needs to inherit from this class.
# File lib/whirled_peas/command/base.rb, line 9 def self.command_name name.split('::').last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase end
# File lib/whirled_peas/command/base.rb, line 13 def self.description end
# File lib/whirled_peas/command/base.rb, line 22 def initialize(args, config) @args = args @config = config end
# File lib/whirled_peas/command/base.rb, line 16 def self.print_usage puts description end
Public Instance Methods
# File lib/whirled_peas/command/base.rb, line 35 def build_logger require 'logger' if config.log_file.is_a?(IO) output = config.log_file else File.open(config.log_file, 'a') end logger = Logger.new(output) logger.level = config.log_level logger.formatter = config.log_formatter logger end
Returns the name of the command as expected by the command line script. By convention, this name is snake case version of the class name, e.g. the command `do_something` would be implemented by `WhirledPeas::Command::DoSomething`, which needs to inherit from this class.
# File lib/whirled_peas/command/base.rb, line 31 def command_name self.class.command_name end
Display the validation error and print a usage statement
# File lib/whirled_peas/command/base.rb, line 58 def print_error puts @error_text if @error_text print_usage end
Commands that inherit from this class must override this method
# File lib/whirled_peas/command/base.rb, line 64 def start end
@return [true|false] true if all of the required options were provided
# File lib/whirled_peas/command/base.rb, line 51 def valid? @error_text = nil validate! @error_text.nil? end
Private Instance Methods
# File lib/whirled_peas/command/base.rb, line 81 def options_usage end
# File lib/whirled_peas/command/base.rb, line 71 def print_usage puts ["Usage: #{$0} #{command_name}", *options_usage].join(' ') end
Commands that inherit from this class can override this method to validate command line options
# File lib/whirled_peas/command/base.rb, line 77 def validate! # Set @error_text if the options are not valid end