| 1 |
module Slop |
| 2 |
class Parser |
| 3 |
|
| 4 |
# Our Options instance. |
| 5 |
attr_reader :options |
| 6 |
|
| 7 |
# A Hash of configuration options. |
| 8 |
attr_reader :config |
| 9 |
|
| 10 |
# Returns an Array of String arguments that were not parsed. |
| 11 |
attr_reader :arguments |
| 12 |
|
| 13 |
def initialize(options, **config) |
| 14 |
@options = options |
| 15 |
@config = config |
| 16 |
reset |
| 17 |
end |
| 18 |
|
| 19 |
# Reset the parser, useful to use the same instance to parse a second |
| 20 |
# time without duplicating state. |
| 21 |
def reset |
| 22 |
@arguments = [] |
| 23 |
@options.each(&:reset) |
| 24 |
self |
| 25 |
end |
| 26 |
|
| 27 |
# Traverse `strings` and process options one by one. Anything after |
| 28 |
# `--` is ignored. If a flag includes a equals (=) it will be split |
| 29 |
# so that `flag, argument = s.split('=')`. |
| 30 |
# |
| 31 |
# The `call` method will be executed immediately for each option found. |
| 32 |
# Once all options have been executed, any found options will have |
| 33 |
# the `finish` method called on them. |
| 34 |
# |
| 35 |
# Returns a Slop::Result. |
| 36 |
def parse(strings) |
| 37 |
reset # reset before every parse |
| 38 |
|
| 39 |
# ignore everything after "--" |
| 40 |
strings, ignored_args = partition(strings) |
| 41 |
|
| 42 |
pairs = strings.each_cons(2).to_a |
| 43 |
# this ensures we still support the last string being a flag, |
| 44 |
# otherwise it'll only be used as an argument. |
| 45 |
pairs << [strings.last, nil] |
| 46 |
|
| 47 |
@arguments = strings.dup |
| 48 |
|
| 49 |
pairs.each_with_index do |pair, idx| |
| 50 |
flag, arg = pair |
| 51 |
break if !flag |
| 52 |
|
| 53 |
# support `foo=bar` |
| 54 |
orig_flag = flag.dup |
| 55 |
if match = flag.match(/([^=]+)=(.*)/) |
| 56 |
flag, arg = match.captures |
| 57 |
end |
| 58 |
|
| 59 |
if opt = try_process(flag, arg) |
| 60 |
# since the option was parsed, we remove it from our |
| 61 |
# arguments (plus the arg if necessary) |
| 62 |
# delete argument first while we can find its index. |
| 63 |
if opt.expects_argument? |
| 64 |
|
| 65 |
# if we consumed the argument, remove the next pair |
| 66 |
if consume_next_argument?(orig_flag) |
| 67 |
pairs.delete_at(idx + 1) |
| 68 |
end |
| 69 |
|
| 70 |
arguments.each_with_index do |argument, i| |
| 71 |
if argument == orig_flag && !orig_flag.include?("=") |
| 72 |
arguments.delete_at(i + 1) |
| 73 |
end |
| 74 |
end |
| 75 |
end |
| 76 |
arguments.delete(orig_flag) |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
@arguments += ignored_args |
| 81 |
|
| 82 |
if !suppress_errors? |
| 83 |
unused_options.each do |o| |
| 84 |
if o.config[:required] |
| 85 |
pretty_flags = o.flags.map { |f| "`#{f}'" }.join(", ") |
| 86 |
raise MissingRequiredOption, "missing required option #{pretty_flags}" |
| 87 |
end |
| 88 |
end |
| 89 |
end |
| 90 |
|
| 91 |
Result.new(self).tap do |result| |
| 92 |
used_options.each { |o| o.finish(result) } |
| 93 |
end |
| 94 |
end |
| 95 |
|
| 96 |
# Returns an Array of Option instances that were used. |
| 97 |
def used_options |
| 98 |
options.select { |o| o.count > 0 } |
| 99 |
end |
| 100 |
|
| 101 |
# Returns an Array of Option instances that were not used. |
| 102 |
def unused_options |
| 103 |
options.to_a - used_options |
| 104 |
end |
| 105 |
|
| 106 |
private |
| 107 |
|
| 108 |
def consume_next_argument?(flag) |
| 109 |
return false if flag.include?("=") |
| 110 |
return true if flag.start_with?("--") |
| 111 |
return true if /\A-[a-zA-Z]\z/ === flag |
| 112 |
false |
| 113 |
end |
| 114 |
|
| 115 |
# We've found an option, process and return it |
| 116 |
def process(option, arg) |
| 117 |
option.ensure_call(arg) |
| 118 |
option |
| 119 |
end |
| 120 |
|
| 121 |
# Try and find an option to process |
| 122 |
def try_process(flag, arg) |
| 123 |
if option = matching_option(flag) |
| 124 |
process(option, arg) |
| 125 |
elsif flag.start_with?("--no-") && option = matching_option(flag.sub("no-", "")) |
| 126 |
process(option, false) |
| 127 |
elsif flag =~ /\A-[^-]{2,}/ |
| 128 |
try_process_smashed_arg(flag) || try_process_grouped_flags(flag, arg) |
| 129 |
else |
| 130 |
if flag.start_with?("-") && !suppress_errors? |
| 131 |
raise UnknownOption.new("unknown option `#{flag}'", "#{flag}") |
| 132 |
end |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
# try and process a flag with a "smashed" argument, e.g. |
| 137 |
# -nFoo or -i5 |
| 138 |
def try_process_smashed_arg(flag) |
| 139 |
option = matching_option(flag[0, 2]) |
| 140 |
if option && option.expects_argument? |
| 141 |
process(option, flag[2..-1]) |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
# try and process as a set of grouped short flags. drop(1) removes |
| 146 |
# the prefixed -, then we add them back to each flag separately. |
| 147 |
def try_process_grouped_flags(flag, arg) |
| 148 |
flags = flag.split("").drop(1).map { |f| "-#{f}" } |
| 149 |
last = flags.pop |
| 150 |
|
| 151 |
flags.each { |f| try_process(f, nil) } |
| 152 |
try_process(last, arg) # send the argument to the last flag |
| 153 |
end |
| 154 |
|
| 155 |
def suppress_errors? |
| 156 |
config[:suppress_errors] |
| 157 |
end |
| 158 |
|
| 159 |
def matching_option(flag) |
| 160 |
options.find { |o| o.flags.include?(flag) } |
| 161 |
end |
| 162 |
|
| 163 |
def partition(strings) |
| 164 |
if strings.include?("--") |
| 165 |
partition_idx = strings.index("--") |
| 166 |
return [[], strings[1..-1]] if partition_idx.zero? |
| 167 |
[strings[0..partition_idx-1], strings[partition_idx+1..-1]] |
| 168 |
else |
| 169 |
[strings, []] |
| 170 |
end |
| 171 |
end |
| 172 |
end |
| 173 |
end |