diff options
author | Lee Jarvis <ljjarvis@gmail.com> | 2014-11-19 09:12:38 +0000 |
---|---|---|
committer | Lee Jarvis <ljjarvis@gmail.com> | 2014-11-19 09:12:47 +0000 |
commit | 253dcd9622328d5f7a5a695c3bf487b65565fb9e (patch) | |
tree | 846b4a6579b3b871558fb271302c114822e46ac9 /lib/slop/parser.rb | |
parent | b91db74b327848841bee30cbecead14c45e3a260 (diff) | |
download | slop-253dcd9622328d5f7a5a695c3bf487b65565fb9e.tar.gz |
Basic option parsing
Diffstat (limited to 'lib/slop/parser.rb')
-rw-r--r-- | lib/slop/parser.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 0ede9d8..3bcb0a9 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -1,5 +1,30 @@ module Slop class Parser + attr_reader :options + def initialize(options) + @options = options + end + + def parse(strings) + pairs = strings.each_cons(2).to_a + pairs << [strings.last, nil] + + pairs.each do |flag, arg| + option = matching_option(flag) + + if option + option.call(arg) + end + end + + Result.new(self) + end + + private + + def matching_option(flag) + options.find { |o| o.flags.include?(flag) } + end end end |