diff options
author | Lee Jarvis <leejarvis@fastmail.com> | 2017-04-29 16:48:47 +0100 |
---|---|---|
committer | Lee Jarvis <leejarvis@fastmail.com> | 2017-04-29 16:48:47 +0100 |
commit | ed049cb2aa7ce30a67661449e75b883f9969722f (patch) | |
tree | b4ddb3b130e05f702eabe51b759ec219afa19e64 /lib | |
parent | 57af528a96d27732202effc039f183b1733cb64d (diff) | |
download | slop-ed049cb2aa7ce30a67661449e75b883f9969722f.tar.gz |
Avoid deleting args after '--'
Everything after -- is sacred and shouldn't be mutated. So lets just
remove it all straight away and add it to our arguments list after we're
done parsing
Closes #194
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/parser.rb | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 142540a..2b20d36 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -36,6 +36,9 @@ module Slop def parse(strings) reset # reset before every parse + # ignore everything after "--" + strings, ignored_args = partition(strings) + pairs = strings.each_cons(2).to_a # this ensures we still support the last string being a flag, # otherwise it'll only be used as an argument. @@ -47,12 +50,6 @@ module Slop flag, arg = pair break if !flag - # ignore everything after '--', flag or not - if flag == '--' - arguments.delete(flag) - break - end - # support `foo=bar` orig_flag = flag.dup orig_arg = arg @@ -81,6 +78,8 @@ module Slop end end + @arguments += ignored_args + Result.new(self).tap do |result| used_options.each { |o| o.finish(result) } end @@ -145,5 +144,14 @@ module Slop def matching_option(flag) options.find { |o| o.flags.include?(flag) } end + + private def partition(strings) + if strings.include?("--") + partition_idx = strings.index("--") + [strings[0..partition_idx-1], strings[partition_idx+1..-1]] + else + [strings, []] + end + end end end |