diff options
author | Kyrylo Silin <kyrylosilin@gmail.com> | 2012-12-23 13:34:43 +0200 |
---|---|---|
committer | Kyrylo Silin <kyrylosilin@gmail.com> | 2012-12-23 13:34:43 +0200 |
commit | ad20e3b5b42210231e7aa6664582e02d46662246 (patch) | |
tree | eceebba0f795b40541dd9194c0c8e22cb63359ee | |
parent | 2e7165230be95906963b600216e0712edbc8e423 (diff) | |
download | slop-ad20e3b5b42210231e7aa6664582e02d46662246.tar.gz |
Don't always append global/default headers
Invokations of `global` or `default` methods with bare blocks (that is,
without option declarations) appends to the `helps` local variable
unwanted information, which results in this:
% my_cli_app --help
Global options
Other options
%
Fix it by checking whether global or default options, actually, have any
options defined.
Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
-rw-r--r-- | lib/slop/commands.rb | 8 | ||||
-rw-r--r-- | test/commands_test.rb | 14 |
2 files changed, 20 insertions, 2 deletions
diff --git a/lib/slop/commands.rb b/lib/slop/commands.rb index abaa913..0e3184b 100644 --- a/lib/slop/commands.rb +++ b/lib/slop/commands.rb @@ -157,8 +157,12 @@ class Slop defaults = commands.delete('default') globals = commands.delete('global') helps = commands.reject { |_, v| v.options.none? } - helps.merge!('Global options' => globals.to_s) if globals - helps.merge!('Other options' => defaults.to_s) if defaults + if globals && globals.options.any? + helps.merge!('Global options' => globals.to_s) + end + if defaults && defaults.options.any? + helps.merge!('Other options' => defaults.to_s) + end banner = @banner ? "#{@banner}\n" : "" banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n") end diff --git a/test/commands_test.rb b/test/commands_test.rb index 174dcf2..6721400 100644 --- a/test/commands_test.rb +++ b/test/commands_test.rb @@ -13,6 +13,16 @@ class CommandsTest < TestCase add_callback(:empty) { 'version 1' } end end + + @empty_commands = Slop::Commands.new do + default do + end + + global do + end + + on 'verbose' + end end test "it nests instances of Slop" do @@ -75,6 +85,10 @@ class CommandsTest < TestCase assert_kind_of Slop, @commands.global end + test "empty default/global blocks don't add their titles in the help output" do + assert_empty @empty_commands.to_s + end + test "parse does nothing when there's nothing to parse" do assert @commands.parse [] end |