Class: DynamicCmdParse

Inherits:
Object
  • Object
show all
Defined in:
lib/opts.rb

Instance Method Summary collapse

Instance Method Details

#parse!(argv = nil, aDefaults = {}) ⇒ Object

destructively parses the ARGV for any dash-starting options without any or with one argument. The options can be validated by validateOpts method. return opts symbol=>string, the ARGV contains the remaining (and unparsed) content



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opts.rb', line 42

def parse! argv = nil, aDefaults = {}
    argv = ARGV unless argv
    key = nil
    rest = []
    opts = aDefaults
    while (argv.length>0 or key) do
        tmp = argv.shift
        new_key = tmp ? tmp.scan(/-+([\w\d\-\_]+)/).flatten.first : nil
        if new_key
            opts[key.intern] = true if key 
            key=new_key
        else
            if key
                values = true
                #are there any values?
                if tmp
                    #is it a list?
                    values = tmp.split ','
                    values = (values.length>1 ? values : values.first )
                end
                opts[key.intern] = values
                key = nil
            else
                rest << tmp
            end
        end
    end
    argv.replace rest
    validateOpts opts, argv
    opts
end

#validateOpts(opts, argv) ⇒ Object

for adding options in subclass



75
76
77
# File 'lib/opts.rb', line 75

def validateOpts opts, argv
    #raise OptionParser::MissingArgument, "lang" if opts[:lang].nil?
end