diff --git a/lib/yawpa.rb b/lib/yawpa.rb index 321a9f5..8dbf696 100644 --- a/lib/yawpa.rb +++ b/lib/yawpa.rb @@ -17,11 +17,29 @@ module Yawpa i = 0 while i < params.length param = params[i] - if param[0] != '-' - args << params[i] + if param =~ /^(-+)(.+)$/ + case $1.length + when 2 + param_key = if options[$2] + $2 + elsif options[$2.to_sym] + $2.to_sym + else + nil + end + if param_key.nil? + raise UnknownOptionException.new("Unknown option '#{param}'") + end + opt_config = options[param_key] + nargs = opt_config[:nargs] || 0 + if nargs == 0 + opts[param_key] = true + elsif nargs.class == FixNum + elsif nargs.class == Range + end + end else - opt_config = options[param] || options[param.to_sym] - raise UnknownOptionException.new("Unknown option '#{param}'") if opt_config.nil? + args << params[i] end i += 1 end diff --git a/spec/yawpa_spec.rb b/spec/yawpa_spec.rb index 5a0ae80..18b310a 100644 --- a/spec/yawpa_spec.rb +++ b/spec/yawpa_spec.rb @@ -15,5 +15,19 @@ describe Yawpa do params = ['one', '--option', 'two'] expect { Yawpa.parse(params, options) }.to raise_error end + + it "returns boolean options which are set" do + options = { + one: {}, + two: {}, + three: {}, + } + params = ['--one', 'arg', '--two', 'arg2'] + opts, args = Yawpa.parse(params, options) + opts.include?(:one).should be_true + opts.include?(:two).should be_true + opts.include?(:three).should be_false + args.should eq(['arg', 'arg2']) + end end end