Class: Smartdict::Commands::AbstractCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/smartdict/commands/abstract_command.rb

Overview

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.

Constant Summary collapse

INDENT_SIZE =

Number of spaces for indent.

2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.

Parameters:

  • args (Array) (defaults to: [])

    arguments passed from the command line



166
167
168
# File 'lib/smartdict/commands/abstract_command.rb', line 166

def initialize(args = [])
  set_arguments_and_options!(args)
end

Class Method Details

.arguments(*argument_names) ⇒ Object

Defines available arguments and their order.



80
81
82
# File 'lib/smartdict/commands/abstract_command.rb', line 80

def self.arguments(*argument_names)
  self.known_arguments = argument_names
end

.default(values) ⇒ Object

Sets default values for arguments.

Parameters:

  • values (Hash)


86
87
88
# File 'lib/smartdict/commands/abstract_command.rb', line 86

def self.default(values)
  self.default_argument_values = values
end

.help_messageString

Returns help message for the command to be displayed.

Returns:

  • (String)

    help message for the command to be displayed.



132
133
134
135
136
# 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_messageString

Returns syntax part of the help message.

Returns:

  • (String)

    syntax part of the help message.



139
140
141
142
143
144
145
# 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_messageString

Returns usage part of the help message.

Returns:

  • (String)

    usage part of the help message.



148
149
150
151
152
153
154
# 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) ⇒ Object

Sets default values for class attributes.



157
158
159
160
161
# File 'lib/smartdict/commands/abstract_command.rb', line 157

def self.inherited(base)
  base.known_arguments ||= []
  base.default_argument_values ||= {}
  base.known_options ||= {}
end

.options(options = {}) ⇒ Object

Defines available options with their default values.

Usage:

options :to => "en",
        :from => lambda { Settings.current_language }

Raises:



94
95
96
97
# 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_nameString

Returns program name. It’s meant to be used in usage examples.

Returns:

  • (String)

    program name. It’s meant to be used in usage examples.



127
128
129
# File 'lib/smartdict/commands/abstract_command.rb', line 127

def self.prog_name
  "smartdict #{name}"
end

.run(args) ⇒ Object

Runs command.

Parameters:

  • args (Array)

    arguments passed from the command line



69
70
71
72
73
74
75
76
77
# 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) ⇒ Object

Sets description message for a command.



105
106
107
# File 'lib/smartdict/commands/abstract_command.rb', line 105

def self.set_description(description)
  self.description = description
end

.set_name(name) ⇒ Object

Defines name of a command.



110
111
112
# File 'lib/smartdict/commands/abstract_command.rb', line 110

def self.set_name(name)
  self.name = name
end

.set_summary(summary) ⇒ Object

Sets summary message for a command.



100
101
102
# File 'lib/smartdict/commands/abstract_command.rb', line 100

def self.set_summary(summary)
  self.summary = summary
end

.set_syntax(syntax) ⇒ Object

Sets syntax message.

Parameters:

  • syntax (String)

    multi line text with number of syntax examples



116
117
118
# File 'lib/smartdict/commands/abstract_command.rb', line 116

def self.set_syntax(syntax)
  self.syntax = syntax
end

.set_usage(usage) ⇒ Object

Sets usage examples

Parameters:

  • usage (String)

    multi line text with number of usage examples.



122
123
124
# File 'lib/smartdict/commands/abstract_command.rb', line 122

def self.set_usage(usage)
  self.usage = usage
end

Instance Method Details

#extract_arguments_and_options(args) ⇒ Object

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# 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) ⇒ Object

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



196
197
198
199
200
201
202
203
204
205
206
207
# 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) ⇒ Object

Parses all passed arguments and initializes @arguments and @options variables.

Parameters:

  • args (Array)

    arguments passed from the command line



172
173
174
175
176
# 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) ⇒ Object

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



211
212
213
214
215
216
217
218
219
220
221
222
223
# 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