Class: Slop::Parser
- Inherits:
-
Object
- Object
- Slop::Parser
- Defined in:
- lib/slop/parser.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns an Array of String arguments that were not parsed.
-
#config ⇒ Object
readonly
A Hash of configuration options.
-
#options ⇒ Object
readonly
Our Options instance.
Instance Method Summary collapse
-
#initialize(options, **config) ⇒ Parser
constructor
A new instance of Parser.
-
#parse(strings) ⇒ Object
Traverse ‘strings` and process options one by one.
-
#reset ⇒ Object
Reset the parser, useful to use the same instance to parse a second time without duplicating state.
-
#unused_options ⇒ Object
Returns an Array of Option instances that were not used.
-
#used_options ⇒ Object
Returns an Array of Option instances that were used.
Constructor Details
#initialize(options, **config) ⇒ Parser
Returns a new instance of Parser.
13 14 15 16 17 |
# File 'lib/slop/parser.rb', line 13 def initialize(, **config) @options = @config = config reset end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns an Array of String arguments that were not parsed.
11 12 13 |
# File 'lib/slop/parser.rb', line 11 def arguments @arguments end |
#config ⇒ Object (readonly)
A Hash of configuration options.
8 9 10 |
# File 'lib/slop/parser.rb', line 8 def config @config end |
#options ⇒ Object (readonly)
Our Options instance.
5 6 7 |
# File 'lib/slop/parser.rb', line 5 def @options end |
Instance Method Details
#parse(strings) ⇒ Object
Traverse ‘strings` and process options one by one. Anything after `–` is ignored. If a flag includes a equals (=) it will be split so that `flag, argument = s.split(’=‘)`.
The ‘call` method will be executed immediately for each option found. Once all options have been executed, any found options will have the `finish` method called on them.
Returns a Slop::Result.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/slop/parser.rb', line 36 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? .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| .each { |o| o.finish(result) } end end |
#reset ⇒ Object
Reset the parser, useful to use the same instance to parse a second time without duplicating state.
21 22 23 24 25 |
# File 'lib/slop/parser.rb', line 21 def reset @arguments = [] @options.each(&:reset) self end |
#unused_options ⇒ Object
Returns an Array of Option instances that were not used.
102 103 104 |
# File 'lib/slop/parser.rb', line 102 def .to_a - end |
#used_options ⇒ Object
Returns an Array of Option instances that were used.
97 98 99 |
# File 'lib/slop/parser.rb', line 97 def .select { |o| o.count > 0 } end |