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/slop/option.rb | |
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/slop/option.rb')
-rw-r--r-- | lib/slop/option.rb | 6 |
1 files changed, 6 insertions, 0 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 |