diff options
author | Lee Jarvis <leejarvis@fastmail.com> | 2017-01-29 21:57:04 +0000 |
---|---|---|
committer | Lee Jarvis <leejarvis@fastmail.com> | 2017-01-29 21:57:04 +0000 |
commit | f9010939a1debceb22152672df2bc1585c57ceec (patch) | |
tree | 9dabc1a5365727ba124c29a90ae90ce6d4367d44 /lib | |
parent | f88f4d87458034771f8e1e5f533fafd5892802b5 (diff) | |
download | slop-f9010939a1debceb22152672df2bc1585c57ceec.tar.gz |
Fix support for parsing -x5
Thanks to @RickHull for the "smashing" terminology and various
conversations
Fixes #199
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/parser.rb | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 0b33d55..142540a 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -111,13 +111,7 @@ module Slop elsif flag.start_with?("--no-") && option = matching_option(flag.sub("no-", "")) process(option, false) elsif flag =~ /\A-[^-]{2,}/ - # try and process as a set of grouped short flags. drop(1) removes - # the prefixed -, then we add them back to each flag separately. - flags = flag.split("").drop(1).map { |f| "-#{f}" } - last = flags.pop - - flags.each { |f| try_process(f, nil) } - try_process(last, arg) # send the argument to the last flag + try_process_smashed_arg(flag) || try_process_grouped_flags(flag, arg) else if flag.start_with?("-") && !suppress_errors? raise UnknownOption.new("unknown option `#{flag}'", "#{flag}") @@ -125,6 +119,25 @@ module Slop end end + # try and process a flag with a "smashed" argument, e.g. + # -nFoo or -i5 + def try_process_smashed_arg(flag) + option = matching_option(flag[0, 2]) + if option && option.expects_argument? + process(option, flag[2..-1]) + end + end + + # try and process as a set of grouped short flags. drop(1) removes + # the prefixed -, then we add them back to each flag separately. + def try_process_grouped_flags(flag, arg) + flags = flag.split("").drop(1).map { |f| "-#{f}" } + last = flags.pop + + flags.each { |f| try_process(f, nil) } + try_process(last, arg) # send the argument to the last flag + end + def suppress_errors? config[:suppress_errors] end |