class Bnet::Command
Attributes
args[RW]
options[RW]
parser[RW]
Public Class Methods
new()
click to toggle source
# File lib/bnet/command.rb, line 27 def initialize @options = OpenStruct.new @parser = OptionParser.new do |opts| opts.banner = <<-END.gsub(/^\s+/, '') #{description}\n Usage: #{File.basename $0} #{name}#{" " + extra_params unless extra_params.nil?} END setup_opts(opts) end end
Public Instance Methods
help()
click to toggle source
# File lib/bnet/command.rb, line 50 def help "* #{(name + ':').ljust(12)}#{parser.help}" end
name()
click to toggle source
# File lib/bnet/command.rb, line 54 def name self.class.name.split('::').last.gsub(/Command$/, '').downcase end
parse_and_run(args)
click to toggle source
# File lib/bnet/command.rb, line 40 def parse_and_run(args) parse args begin run rescue Bnet::BadInputError, Bnet::RequestFailedError => e raise Bnet::InvalidCommandException.new(e.message, name.to_sym) end end
Protected Instance Methods
description()
click to toggle source
# File lib/bnet/command.rb, line 73 def description # description of the command end
extra_params()
click to toggle source
# File lib/bnet/command.rb, line 77 def extra_params # extra params of the command end
run()
click to toggle source
# File lib/bnet/command.rb, line 85 def run # can access @options end
setup_opts(opts)
click to toggle source
# File lib/bnet/command.rb, line 81 def setup_opts(opts) # fill @options end
Private Instance Methods
parse(args)
click to toggle source
# File lib/bnet/command.rb, line 60 def parse(args) begin parser.parse! args rescue OptionParser::InvalidOption => e e.class.module_eval { attr_accessor :command } unless e.respond_to? :command e.command = name.to_sym raise e end @args = args end