class CmdParser

Public Class Methods

assert() { || ... } click to toggle source
# File lib/commons.rb, line 237
def self.assert
    error_message = yield
    if error_message
        puts error_message
        exit
    end
end
assert_not_set(forbidden_key, actual_param, forbidden_param, message="incompatible commands") click to toggle source
# File lib/commons.rb, line 245
def self.assert_not_set(forbidden_key, actual_param, forbidden_param, message="incompatible commands")
    if CmdParser.opt(forbidden_key) != nil
        puts "Do not use #{forbidden_param} with #{actual_param} (#{message}). Aborting."
        exit
    end
end
autoset_flag(short, long, description, flag, incompatibles=[]) click to toggle source
# File lib/commons.rb, line 215
def self.autoset_flag(short, long, description, flag, incompatibles=[])
    @@bindings[flag] = short
    @@opts.on(short, long, description) do
        incompatibles.each do |incompatible|
            CmdParser.assert_not_set incompatible, short, @@bindings[incompatible]
        end
        
        CmdParser.set flag, true
    end
end
autoset_param(short, long, description, flag, incompatibles=[]) click to toggle source
# File lib/commons.rb, line 226
def self.autoset_param(short, long, description, flag, incompatibles=[])
    @@bindings[flag] = short
    @@opts.on(short, long, description) do |v|
        incompatibles.each do |incompatible|
            CmdParser.assert_not_set incompatible, short, @@bindings[incompatible]
        end
        
        CmdParser.set flag, v
    end
end
opt(key) click to toggle source
# File lib/commons.rb, line 179
def self.opt(key)
    return @@data[key]
end
set(key, value) click to toggle source
# File lib/commons.rb, line 183
def self.set(key, value)
    @@data[key] = value
end
setup(cmd, *args) { |opts| ... } click to toggle source
# File lib/commons.rb, line 187
def self.setup(cmd, *args)
    parser = OptionParser.new do |opts|
        opts.banner = "Usage: #{cmd} [options] #{args.join " "}"
        
        @@opts = opts
        yield opts
        @@opts = nil
        
        opts.on("-v", "--verbose", "Writes a lot of information") do
            CmdParser.set :verbose, true
        end
        
        opts.on("-h", "--help", "Prints this help") do
            puts opts
            exit
        end
    end
    
    parser.parse! ARGV
    
    arguments = {}
    for i in 0...args.size
        arguments[args[i]] = ARGV[i]
    end
    
    return arguments
end