Class: Smartdict::Commands::AbstractCommand
- Inherits:
-
Object
- Object
- Smartdict::Commands::AbstractCommand
- 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.
: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.
Direct Known Subclasses
Constant Summary collapse
- INDENT_SIZE =
Number of spaces for indent.
2
Class Method Summary collapse
-
.arguments(*argument_names) ⇒ Object
Defines available arguments and their order.
-
.default(values) ⇒ Object
Sets default values for arguments.
-
.help_message ⇒ String
Help message for the command to be displayed.
-
.help_syntax_message ⇒ String
Syntax part of the help message.
-
.help_usage_message ⇒ String
Usage part of the help message.
-
.inherited(base) ⇒ Object
Sets default values for class attributes.
-
.options(options = {}) ⇒ Object
Defines available options with their default values.
-
.prog_name ⇒ String
Program name.
-
.run(args) ⇒ Object
Runs command.
-
.set_description(description) ⇒ Object
Sets description message for a command.
-
.set_name(name) ⇒ Object
Defines name of a command.
-
.set_summary(summary) ⇒ Object
Sets summary message for a command.
-
.set_syntax(syntax) ⇒ Object
Sets syntax message.
-
.set_usage(usage) ⇒ Object
Sets usage examples.
Instance Method Summary collapse
-
#extract_arguments_and_options(args) ⇒ Object
Splits input args to arguments and options.
-
#initialize(args = []) ⇒ AbstractCommand
constructor
A new instance of AbstractCommand.
-
#set_arguments!(arg_values) ⇒ Object
Initializes @arguments variable.
-
#set_arguments_and_options!(args) ⇒ Object
Parses all passed arguments and initializes @arguments and @options variables.
-
#set_options!(options) ⇒ Object
Initializes @options variable.
Constructor Details
#initialize(args = []) ⇒ AbstractCommand
Returns a new instance of AbstractCommand.
166 167 168 |
# File 'lib/smartdict/commands/abstract_command.rb', line 166 def initialize(args = []) (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.
86 87 88 |
# File 'lib/smartdict/commands/abstract_command.rb', line 86 def self.default(values) self.default_argument_values = values end |
.help_message ⇒ String
Returns help message for the command to be displayed.
132 133 134 135 136 |
# File 'lib/smartdict/commands/abstract_command.rb', line 132 def self. = "#{description}\n\n" << "#{}\n" << "#{}\n" end |
.help_syntax_message ⇒ String
Returns syntax part of the help message.
139 140 141 142 143 144 145 |
# File 'lib/smartdict/commands/abstract_command.rb', line 139 def self. result = " " * INDENT_SIZE + "Syntax:\n" syntax.split("\n").map do |line| result << " " * INDENT_SIZE * 2 + "#{line.strip}\n" end result end |
.help_usage_message ⇒ String
Returns usage part of the help message.
148 149 150 151 152 153 154 |
# File 'lib/smartdict/commands/abstract_command.rb', line 148 def self. 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. ||= {} end |
.options(options = {}) ⇒ Object
Defines available options with their default values.
Usage:
:to => "en",
:from => lambda { Settings.current_language }
94 95 96 97 |
# File 'lib/smartdict/commands/abstract_command.rb', line 94 def self.( = {}) raise Smartdict::Error.new("options must be a hash") unless .is_a? Hash self. = end |
.prog_name ⇒ String
Returns 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.
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 else self.new(args).execute end rescue Smartdict::Error => err puts err. 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.
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
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 (args) arguments = [] = {} args = args.dup while value = args.shift if match = value.match(/^--(\w+)/) [match[1].to_sym] = args.shift else arguments << value end end [arguments, ] 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.
172 173 174 175 176 |
# File 'lib/smartdict/commands/abstract_command.rb', line 172 def (args) arguments, = (args) set_arguments!(arguments) () 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 () @options = {} .each do |opt_name, default_value| value = [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 |