lib/option/parser.rb in bake-toolkit-1.0.9 vs lib/option/parser.rb in bake-toolkit-1.0.10
- old
+ new
@@ -19,36 +19,44 @@
class Parser
def initialize(argv)
@options = {}
@argv = argv
+ @default = nil
end
def add_option(opt)
@options[opt.param] = opt
end
+ def add_default(opt)
+ @default = opt
+ end
+
def parse_internal(ignoreInvalid = true)
pos = 0
begin
while pos < @argv.length do
if not @options.include?@argv[pos]
- if ignoreInvalid
- pos = pos + 1
- next
+ if @default
+ if not @default.call(@argv[pos])
+ raise "Option #{@argv[pos]} unknown"
+ end
+ elsif not ignoreInvalid
+ raise "Option #{@argv[pos]} unknown"
end
- raise "Option #{@argv[pos]} unknown"
- end
- option = @options[@argv[pos]]
- if option.arg
- if pos+1 < @argv.length and @argv[pos+1][0] != "-"
- option.block.call(@argv[pos+1])
- pos = pos + 1
+ else
+ option = @options[@argv[pos]]
+ if option.arg
+ if pos+1 < @argv.length and @argv[pos+1][0] != "-"
+ option.block.call(@argv[pos+1])
+ pos = pos + 1
+ else
+ raise "Argument for option #{@argv[pos]} missing"
+ end
else
- raise "Argument for option #{@argv[pos]} missing"
+ option.block.call()
end
- else
- option.block.call()
end
pos = pos + 1
end
rescue SystemExit => e
raise
\ No newline at end of file