diff options
author | Lee Jarvis <ljjarvis@gmail.com> | 2014-12-17 08:26:50 +0000 |
---|---|---|
committer | Lee Jarvis <ljjarvis@gmail.com> | 2014-12-17 08:26:50 +0000 |
commit | f77101e3fef89fd70ae0db3708ce623b7d8d32c1 (patch) | |
tree | 54635808da506585eefe8de2fdda53a3f17498cd /lib | |
parent | 3bd5ec4eabf14dc89a14b285bd5ccf1c0a45d6a5 (diff) | |
download | slop-f77101e3fef89fd70ae0db3708ce623b7d8d32c1.tar.gz |
Add NullOption and default to using it
A NullOption is one whos return value we don't care about. For example,
you might just want a `--version` option which simply prints the
version and exits. In this case, having a `true` value in `to_hash` is
really just noise. We probably don't care about it, using NullOption
discards it.
I think using this for Options#on makes sense because this is logical:
opts.on '--version' do
puts VERSION
exit
end
Rather than:
opts.add '--version' do
puts VERSION
exit
end
And defaulting to a StringOption. This also means you have to be
explicit about adding such an option, which is a good thing.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/option.rb | 6 | ||||
-rw-r--r-- | lib/slop/options.rb | 25 | ||||
-rw-r--r-- | lib/slop/result.rb | 2 | ||||
-rw-r--r-- | lib/slop/types.rb | 8 |
4 files changed, 37 insertions, 4 deletions
diff --git a/lib/slop/option.rb b/lib/slop/option.rb index 089d36b..85d7cb7 100644 --- a/lib/slop/option.rb +++ b/lib/slop/option.rb @@ -53,6 +53,12 @@ module Slop true end + # Override this if you want to ignore the return value for an option + # (i.e so Result#to_hash does not include it). + def null? + false + end + def value @value || default_value end diff --git a/lib/slop/options.rb b/lib/slop/options.rb index f89e857..34ebe86 100644 --- a/lib/slop/options.rb +++ b/lib/slop/options.rb @@ -4,7 +4,7 @@ module Slop DEFAULT_CONFIG = { suppress_errors: false, - type: "string", + type: "null", } attr_reader :options @@ -23,7 +23,21 @@ module Slop yield self if block_given? end - def add(*flags, **config, &block) + # Add a new option. This method is an alias for adding a NullOption + # (i.e an option with an ignored return value). + # + # Example: + # + # opts = Slop.parse do |o| + # o.on '--version' do + # puts Slop::VERSION + # end + # end + # + # opts.to_hash #=> {} + # + # Returns the newly created Option subclass. + def on(*flags, **config, &block) desc = flags.pop unless flags.last.start_with?('-') config = self.config.merge(config) klass = Slop.string_to_option_class(config[:type].to_s) @@ -32,6 +46,8 @@ module Slop add_option option end + # Add a separator between options. Used when displaying + # the help text. def separator(string) if separators[options.size] separators.last << "\n#{string}" @@ -40,10 +56,12 @@ module Slop end end + # Sugar to avoid `options.parser.parse(x)`. def parse(strings) parser.parse(strings) end + # Implements the Enumerable interface. def each(&block) options.each(&block) end @@ -51,7 +69,7 @@ module Slop def method_missing(name, *args, **config, &block) if respond_to_missing?(name) config[:type] = name - add(*args, config, &block) + on(*args, config, &block) else super end @@ -65,6 +83,7 @@ module Slop options.dup end + # Returns the help text for this options. Used by Result#to_s. def to_s(prefix: " " * 4) str = banner + "\n" len = longest_flag_length diff --git a/lib/slop/result.rb b/lib/slop/result.rb index 23d023b..03b369b 100644 --- a/lib/slop/result.rb +++ b/lib/slop/result.rb @@ -50,7 +50,7 @@ module Slop # Returns a hash with option key => value. def to_hash - Hash[options.map { |o| [o.key, o.value] }] + Hash[options.reject(&:null?).map { |o| [o.key, o.value] }] end alias to_h to_hash diff --git a/lib/slop/types.rb b/lib/slop/types.rb index 8576421..c7db245 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -41,4 +41,12 @@ module Slop config[:delimiter] || "," end end + + # an option that discards the return value + class NullOption < BoolOption + def null? + true + end + end + end |