Class: Ampt::Command
Overview
Class that is created to hold each Ampt command.
Instance Method Summary collapse
-
#arg(str) ⇒ Object
Specify that a command takes an argument without flags.
-
#conflicts(*opts) ⇒ Object
Specify that one or more options conflict with each other.
-
#depends(*opts) ⇒ Object
Specify that one or more options depend on each other.
-
#desc(description = nil, options = {}) ⇒ Object
Provide a description for the command.
-
#die(message) ⇒ Object
If for any reason you need Ampt to quit (error, invalid args), call this.
-
#initialize(config, name, &b) ⇒ Command
constructor
Create a new command.
-
#on_run(&b) ⇒ Object
Specify the behavior of the command when it is executed.
-
#opt(name, desc, options = {}) ⇒ Object
Specify that a command takes an opt with the given name and description, and possibly some options.
-
#reconfig(&b) ⇒ Object
Called if you define a command twice in a configuration file.
-
#run(args) ⇒ Object
Executed when a command is run.
Methods included from API
#auth, #history, #list, #player_status, #purge, #random, #recent, #req, #reset, #search, #shuffle_votes, #skip, #song_list, #start, #status, #stop, #unvote, #volume, #vote, #vote_to_top
Constructor Details
#initialize(config, name, &b) ⇒ Command
Create a new command. This is usually not called directly; it is invoked by Ampt::Config::on_run.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/ampt.rb', line 197 def initialize config, name, &b @name = name @desc = [] @arg = [''] @opts = [] @conflicts = [] @depends = [] @on_run = lambda {} @config = config (class << self; self; end).instance_eval do include config.__user end cloaker(&b).bind(self).call end |
Instance Method Details
#arg(str) ⇒ Object
Specify that a command takes an argument without flags. This is only used for nicely formatting the help command.
214 215 216 |
# File 'lib/ampt.rb', line 214 def arg str @arg << '[' + str + ']' end |
#conflicts(*opts) ⇒ Object
Specify that one or more options conflict with each other.
231 232 233 |
# File 'lib/ampt.rb', line 231 def conflicts *opts @conflicts << opts end |
#depends(*opts) ⇒ Object
Specify that one or more options depend on each other.
236 237 238 |
# File 'lib/ampt.rb', line 236 def depends *opts @depends << opts end |
#desc(description = nil, options = {}) ⇒ Object
Provide a description for the command. If called with no arguments, returns the current description. Can be called multiple times, and will append the the previous call. Specify :overwrite => true if you want to overwrite all of the previous text.
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ampt.rb', line 281 def desc description = nil, = {} if description if [:overwrite] @desc = [description] else @desc << description end else @desc end end |
#die(message) ⇒ Object
If for any reason you need Ampt to quit (error, invalid args), call this.
246 247 248 |
# File 'lib/ampt.rb', line 246 def die Trollop::die "ampt #{@name}: #{}" end |
#on_run(&b) ⇒ Object
Specify the behavior of the command when it is executed. Takes a block.
241 242 243 |
# File 'lib/ampt.rb', line 241 def on_run &b @on_run = b end |
#opt(name, desc, options = {}) ⇒ Object
Specify that a command takes an opt with the given name and description, and possibly some options. See Trollop::Opt.
226 227 228 |
# File 'lib/ampt.rb', line 226 def opt name, desc, = {} @opts << [name, desc, ] end |
#reconfig(&b) ⇒ Object
Called if you define a command twice in a configuration file. It will reconfigure the command. This is usually not called manually.
220 221 222 |
# File 'lib/ampt.rb', line 220 def reconfig &b cloaker(&b).bind(self).call end |
#run(args) ⇒ Object
Executed when a command is run. Usually not called manually.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/ampt.rb', line 251 def run args opts = Trollop:: args, @name, @desc, @arg, @opts, @conflicts, @depends do |*params| name, description, arg, opts, conflicts, depends = *params "ampt #{name}#{arg.join(' ')}: #{description.join " "}" "" # Parse each of the specified opts opts.each do |o| name, desc, = o opt name, desc, end # Parse each of the conflicts conflicts.each do |c| conflicts(*c) end # Parse each of the depends' depends.each do |d| depends(*d) end end @base_url = @config.base_url # Call the on_run block with the options and arguments. @on_run.call(opts, args) end |