diff options
author | Lee Jarvis <ljjarvis@gmail.com> | 2014-12-17 15:12:19 +0000 |
---|---|---|
committer | Lee Jarvis <ljjarvis@gmail.com> | 2014-12-17 15:12:19 +0000 |
commit | cacbe70cd07487716de8401cc4e2ac399f19d537 (patch) | |
tree | e0d3328ae8519fc938a193501098961b3b62c906 /lib | |
parent | a13b9578359f21e14f5a2fa5274a6edd59cd64a1 (diff) | |
download | slop-cacbe70cd07487716de8401cc4e2ac399f19d537.tar.gz |
Comments and clean up
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/option.rb | 20 | ||||
-rw-r--r-- | lib/slop/options.rb | 17 | ||||
-rw-r--r-- | lib/slop/parser.rb | 12 |
3 files changed, 45 insertions, 4 deletions
diff --git a/lib/slop/option.rb b/lib/slop/option.rb index 870c518..5dc8156 100644 --- a/lib/slop/option.rb +++ b/lib/slop/option.rb @@ -4,7 +4,24 @@ module Slop help: true } - attr_reader :flags, :desc, :config, :count, :block + # An Array of flags this option matches. + attr_reader :flags + + # A custom description used for the help text. + attr_reader :desc + + # A Hash of configuration options. + attr_reader :config + + # An Integer count for the total times this option + # has been executed. + attr_reader :count + + # A custom proc that yields the option value when + # it's executed. + attr_reader :block + + # The end value for this option. attr_writer :value def initialize(flags, desc, **config, &block) @@ -12,7 +29,6 @@ module Slop @desc = desc @config = DEFAULT_CONFIG.merge(config) @block = block - reset end diff --git a/lib/slop/options.rb b/lib/slop/options.rb index 34ebe86..c612997 100644 --- a/lib/slop/options.rb +++ b/lib/slop/options.rb @@ -7,10 +7,19 @@ module Slop type: "null", } + # The Array of Option instances we've created. attr_reader :options + + # An Array of separators used for the help text. attr_reader :separators + + # Our Parser instance. attr_reader :parser + + # A Hash of configuration options. attr_reader :config + + # The String banner prefixed to the help string. attr_accessor :banner def initialize(**config) @@ -66,6 +75,8 @@ module Slop options.each(&block) end + # Handle custom option types. Will fall back to raising an + # exception if an option is not defined. def method_missing(name, *args, **config, &block) if respond_to_missing?(name) config[:type] = name @@ -79,6 +90,7 @@ module Slop Slop.option_defined?(name) || super end + # Return a copy of our options Array. def to_a options.dup end @@ -89,9 +101,11 @@ module Slop len = longest_flag_length options.select(&:help?).each_with_index do |opt, i| + # use the index to fetch an associated separator if sep = separators[i] str << "#{sep}\n" end + str << "#{prefix}#{opt.to_s(offset: len)}\n" end @@ -111,6 +125,9 @@ module Slop def add_option(option) options.each do |o| flags = o.flags & option.flags + + # Raise an error if we found an existing option with the same + # flags. I can't immediately see a use case for this.. if flags.any? raise ArgumentError, "duplicate flags: #{flags}" end diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 85a9428..3a828a0 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -1,11 +1,15 @@ module Slop class Parser - attr_reader :options, :config + + # Our Options instance. + attr_reader :options + + # A Hash of configuration options. + attr_reader :config def initialize(options, **config) @options = options @config = config - reset end @@ -27,11 +31,15 @@ module Slop # Returns a Slop::Result. def parse(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] pairs.each do |flag, arg| + # ignore everything after '--', flag or not break if !flag || flag == '--' + # support `foo=bar` if flag.include?("=") flag, arg = flag.split("=") end |