lib/args_parser/parser.rb in args_parser-0.1.6 vs lib/args_parser/parser.rb in args_parser-0.2.0
- old
+ new
@@ -1,28 +1,18 @@
-
module ArgsParser
- def self.parse(argv=[], config={}, &block)
- Config.default.each do |k,v|
- config[k] = v unless config[k]
- end
- parser = Parser.new(config, &block)
- parser.parse argv
- parser
- end
class Parser
attr_reader :argv, :params, :aliases
public
def first
argv.first
end
def initialize(config, &block)
- unless block_given?
- raise ArgumentError, 'initialize block was not given'
- end
+ raise ArgumentError, 'initialize block was not given' unless block_given?
+
@config = config
@argv = []
@params = Hash.new{|h,k|
h[k] = {
:default => nil,
@@ -33,10 +23,11 @@
}
}
@aliases = {}
@filter = Filter.new
@validator = Validator.new
+ @ons = []
filter do |v|
(v.kind_of? String and v =~ /^-?\d+$/) ? v.to_i : v
end
filter do |v|
@@ -48,11 +39,11 @@
on_validate_error do |err, name, value|
STDERR.puts "Error: #{err.message} (--#{name} #{value})"
exit 1
end
- instance_eval(&block)
+ instance_eval &block
end
private
def arg(name, description, opts={})
name = name.to_sym
@@ -62,49 +53,56 @@
params[name][:alias] = opts[:alias]
aliases[opts[:alias]] = name if opts[:alias]
end
def filter(name=nil, &block)
- if block_given?
- @filter.add name, block
- end
+ @filter.add name, block if block_given?
end
def on_filter_error(err=nil, name=nil, value=nil, &block)
if block_given?
@on_filter_error = block
- else
- @on_filter_error.call(err, name, value) if @on_filter_error
+ elsif @on_filter_error.kind_of? Proc
+ @on_filter_error.call(err, name, value)
end
end
def validate(name, message, &block)
- if block_given?
- @validator.add name, message, block
- end
+ @validator.add name, message, block if block_given?
end
def on_validate_error(err=nil, name=nil, value=nil, &block)
if block_given?
@on_validate_error = block
+ elsif @on_validate_error.kind_of? Proc
+ @on_validate_error.call(err, name, value)
+ end
+ end
+
+ def on(name, &block)
+ name = name.to_sym
+ if block_given?
+ @ons.push :name => name, :callback => block
else
- @on_validate_error.call(err, name, value) if @on_validate_error
+ @ons.each do |event|
+ event[:callback].call(self[name]) if event[:name] == name
+ end
end
end
def default(key)
d = params[key.to_sym][:default]
- (d and d.kind_of? Proc) ? d.call : d
+ d.kind_of?(Proc) ? d.call : d
end
public
def args
params.keys
end
def parse(argv)
- method("parse_style_#{@config[:style]}".to_sym).call(argv)
+ send "parse_style_#{@config[:style]}", argv
params.each do |name, param|
next if [nil, true].include? param[:value]
begin
param[:value] = @filter.filter name, param[:value]
rescue => e
@@ -117,31 +115,29 @@
end
if msg
on_validate_error ValidationError.new(msg), name, param[:value]
end
end
+ params.keys.each do |name|
+ on name if has_option? name or has_param? name
+ end
end
def [](key)
- params[key.to_sym][:value] or default(key)
+ params[key.to_sym][:value] || default(key)
end
def []=(key, value)
params[key.to_sym][:value] = value
end
- def has_option?(*opt)
- !(opt.flatten.map{|i|
- self[i] == true
- }.include? false)
+ def has_option?(*opts)
+ !opts.flatten.find{|i| self[i] != true}
end
- def has_param?(*param_)
- !(param_.flatten.map{|i|
- v = self[i]
- (v != nil and v != true) ? true : false
- }.include? false)
+ def has_param?(*params)
+ !params.flatten.find{|i| self[i] == nil or self[i] == true }
end
def inspect
h = Hash.new
params.each do |k,v|
@@ -152,27 +148,23 @@
def help
params_ = Array.new
params.each do |k,v|
v[:name] = k
- params_ << v
+ params_.push v
end
- params_ = params_.delete_if{|i|
- i[:index] < 0
- }.sort{|a,b|
- a[:index] <=> b[:index]
- }
+ params_ = params_.reject{|i| i[:index] < 0 }.sort{|a,b| a[:index] <=> b[:index] }
len = params_.map{|i|
- line = " -#{i[:name]}"
- line += " (-#{i[:alias]})" if i[:alias]
- line.size
+ (i[:alias] ?
+ " -#{i[:name]} (-#{i[:alias]})" :
+ " -#{i[:name]}").size
}.max
"options:\n" + params_.map{|i|
- line = " -#{i[:name]}"
- line += " (-#{i[:alias]})" if i[:alias]
- line = line.ljust(len+2)
+ line = (i[:alias] ?
+ " -#{i[:name]} (-#{i[:alias]})" :
+ " -#{i[:name]}").ljust(len+2)
line += i[:description].to_s
line += " : default - #{default i[:name]}" if i[:default]
line
}.join("\n")
end