class Smartdict::Commands::AbstractCommand

Basic class for all command classes.

Usage:

class Smartdict::Commands::HelloCommand < Smartdict::Commands::AbstractCommand
  # arguments and their default values
  arguments :name
  default   :name => "world"

  # options and their default values.
  options :greating => "Hello",
          :today => lambda { Time.now.strftime("%A") }

  # Other helpful information about the command
  set_name        "hello"
  set_summary     "Summary for the hello command"
  set_description "Demonstrates how Command class works"
  set_syntax      "#{prog_name} NAME [--greating GREATING] [--today DAY]"
  set_usage <<-USAGE
    #{prog_name}
    #{prog_name} Sergey
    #{prog_name} --today Friday
  USAGE

  # This method runs when command executes.
  def execute
    puts "#{@options[:greating]} #{@arguments[:name]}! Today is #{@options[:today]}."
  end
end

# == Output:
# smartdict hello
# Hello world! Today is Monday.
#
# smartdict hello Sergey
# Hello Sergey! Today is Monday.
#
# smartdict hello Sergey --today Friday
# Hello Sergey! Today is Friday.

Constants

INDENT_SIZE

Number of spaces for indent.

Public Class Methods

arguments(*argument_names) click to toggle source

Defines available arguments and their order.

# File lib/smartdict/commands/abstract_command.rb, line 80
def self.arguments(*argument_names)
  self.known_arguments = argument_names
end
default(values) click to toggle source

Sets default values for arguments. @param [Hash] values

# File lib/smartdict/commands/abstract_command.rb, line 86
def self.default(values)
  self.default_argument_values = values
end
help_message() click to toggle source

@return [String] help message for the command to be displayed.

# File lib/smartdict/commands/abstract_command.rb, line 132
def self.help_message
  message = "#{description}\n\n"
  message << "#{help_syntax_message}\n"
  message << "#{help_usage_message}\n"
end
help_syntax_message() click to toggle source

@return [String] syntax part of the help message.

# File lib/smartdict/commands/abstract_command.rb, line 139
def self.help_syntax_message
  result = " " * INDENT_SIZE + "Syntax:\n"
  syntax.split("\n").map do |line|
    result << " " * INDENT_SIZE * 2 + "#{line.strip}\n"
  end
  result
end
help_usage_message() click to toggle source

@return [String] usage part of the help message.

# File lib/smartdict/commands/abstract_command.rb, line 148
def self.help_usage_message
  result = " " * INDENT_SIZE + "Usage:\n"
  usage.split("\n").map do |line|
    result << " " * INDENT_SIZE * 2 + "#{line.strip}\n"
  end
  result
end
inherited(base) click to toggle source

Sets default values for class attributes.

# File lib/smartdict/commands/abstract_command.rb, line 157
def self.inherited(base)
  base.known_arguments ||= []
  base.default_argument_values ||= {}
  base.known_options ||= {}
end
new(args = []) click to toggle source

@param [Array] args arguments passed from the command line

# File lib/smartdict/commands/abstract_command.rb, line 166
def initialize(args = [])
  set_arguments_and_options!(args)
end
options(options = {}) click to toggle source

Defines available options with their default values.

Usage:

options :to => "en",
        :from => lambda { Settings.current_language }
# File lib/smartdict/commands/abstract_command.rb, line 94
def self.options(options = {})
  raise Smartdict::Error.new("options must be a hash") unless options.is_a? Hash
  self.known_options = options
end
prog_name() click to toggle source

@return [String] program name. It’s meant to be used in usage examples.

# File lib/smartdict/commands/abstract_command.rb, line 127
def self.prog_name
  "smartdict #{name}"
end
run(args) click to toggle source

Runs command. @param [Array] args arguments passed from the command line

# File lib/smartdict/commands/abstract_command.rb, line 69
def self.run(args)
  if ['--help', '-h'].include?(args.first)
    puts help_message
  else
    self.new(args).execute
  end
rescue Smartdict::Error => err
  puts err.message
end
set_description(description) click to toggle source

Sets description message for a command.

# File lib/smartdict/commands/abstract_command.rb, line 105
def self.set_description(description)
  self.description = description
end
set_name(name) click to toggle source

Defines name of a command.

# File lib/smartdict/commands/abstract_command.rb, line 110
def self.set_name(name)
  self.name = name
end
set_summary(summary) click to toggle source

Sets summary message for a command.

# File lib/smartdict/commands/abstract_command.rb, line 100
def self.set_summary(summary)
  self.summary = summary
end
set_syntax(syntax) click to toggle source

Sets syntax message. @param [String] syntax multi line text with number of syntax examples

# File lib/smartdict/commands/abstract_command.rb, line 116
def self.set_syntax(syntax)
  self.syntax = syntax
end
set_usage(usage) click to toggle source

Sets usage examples @param [String] usage multi line text with number of usage examples.

# File lib/smartdict/commands/abstract_command.rb, line 122
def self.set_usage(usage)
  self.usage = usage
end

Public Instance Methods

extract_arguments_and_options(args) click to toggle source

Splits input args to arguments and options. Returns arguments as an array and options as a hash.

# File lib/smartdict/commands/abstract_command.rb, line 180
def extract_arguments_and_options(args)
  arguments = []
  options = {}
  args = args.dup
  while value = args.shift
    if match = value.match(/^--(\w+)/)
      options[match[1].to_sym] = args.shift
    else
      arguments << value
    end
  end
  [arguments, options]
end
set_arguments!(arg_values) click to toggle source

Initializes @arguments variable. If no argument was passed then it uses default value.

# File lib/smartdict/commands/abstract_command.rb, line 196
def set_arguments!(arg_values)
  @arguments = {}
  known_arguments.each_with_index do |arg_name, index|
    if value = arg_values[index]
      @arguments[arg_name.to_sym] = value
    elsif default_argument_values.has_key?(arg_name.to_sym)
      @arguments[arg_name.to_sym] = default_argument_values[arg_name.to_sym]
    else
      raise Smartdict::Error.new("Argument `#{arg_name}` is not passed")
    end
  end
end
set_arguments_and_options!(args) click to toggle source

Parses all passed arguments and initializes @arguments and @options variables. @param [Array] args arguments passed from the command line

# File lib/smartdict/commands/abstract_command.rb, line 172
def set_arguments_and_options!(args)
  arguments, options = extract_arguments_and_options(args)
  set_arguments!(arguments)
  set_options!(options)
end
set_options!(options) click to toggle source

Initializes @options variable. If no argument was passed then it uses default value.

# File lib/smartdict/commands/abstract_command.rb, line 211
def set_options!(options)
  @options = {}
  known_options.each do |opt_name, default_value|
    value = options[opt_name]
    unless value
      value = case default_value
        when Proc then default_value.call
        else default_value
      end
    end
    @options[opt_name] = value
  end
end