class Slop::Parser
def parse(strings)
the `finish` method called on them.
Once all options have been executed, any found options will have
The `call` method will be executed immediately for each option found.
so that `flag, argument = s.split('=')`.
`--` is ignored. If a flag includes a equals (=) it will be split
Traverse `strings` and process options one by one. Anything after
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. pairs << [strings.last, nil] @arguments = strings.dup pairs.each_with_index do |pair, idx| flag, arg = pair break if !flag # support `foo=bar` orig_flag = flag.dup if match = flag.match(/([^=]+)=(.*)/) flag, arg = match.captures end if opt = try_process(flag, arg) # since the option was parsed, we remove it from our # arguments (plus the arg if necessary) # delete argument first while we can find its index. if opt.expects_argument? # if we consumed the argument, remove the next pair if consume_next_argument?(orig_flag) pairs.delete_at(idx + 1) end arguments.each_with_index do |argument, i| if argument == orig_flag && !orig_flag.include?("=") arguments.delete_at(i + 1) end end end arguments.delete(orig_flag) end end @arguments += ignored_args if !suppress_errors? unused_options.each do |o| if o.config[:required] pretty_flags = o.flags.map { |f| "`#{f}'" }.join(", ") raise MissingRequiredOption, "missing required option #{pretty_flags}" end end end Result.new(self).tap do |result| used_options.each { |o| o.finish(result) } end end