summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Jarvis <leejarvis@fastmail.com>2020-07-10 08:36:54 +0100
committerLee Jarvis <leejarvis@fastmail.com>2020-07-10 08:36:54 +0100
commite5615fbdd7c4cd07e8fb0c1109fc6da56981829f (patch)
tree36f5e303143b1fe739a490cc5df0ddc75a9790c9
parent53e5d15c0493f8eaa31ea9d3a0d785dc9bb0cbc1 (diff)
downloadslop-e5615fbdd7c4cd07e8fb0c1109fc6da56981829f.tar.gz
Fix bug with separators around `help: false` opts
The separators rely on the indexes of the options before/after them, but we ignore `help: false` options when building the help string, so all of the positions are messed up. This change avoids ignoring those options during the iteration, and instead just doesn't append the option help to the string. Fixes #253
-rw-r--r--lib/slop/options.rb4
-rw-r--r--test/options_test.rb11
2 files changed, 13 insertions, 2 deletions
diff --git a/lib/slop/options.rb b/lib/slop/options.rb
index 9a5f5cc..875fcfe 100644
--- a/lib/slop/options.rb
+++ b/lib/slop/options.rb
@@ -102,13 +102,13 @@ module Slop
str = config[:banner] ? "#{banner}\n" : ""
len = longest_flag_length
- options.select(&:help?).each_with_index.sort_by{ |o,i| [o.tail, i] }.each do |opt, i|
+ options.select.each_with_index.sort_by{ |o,i| [o.tail, i] }.each 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"
+ str << "#{prefix}#{opt.to_s(offset: len)}\n" if opt.help?
end
if sep = separators[options.size]
diff --git a/test/options_test.rb b/test/options_test.rb
index 2f83fc4..0aa6e67 100644
--- a/test/options_test.rb
+++ b/test/options_test.rb
@@ -70,6 +70,17 @@ describe Slop::Options do
assert_equal [""], @options.separators
end
+
+ it "correctly handles options with `help: false`" do
+ @options.boolean "--opt1"
+ @options.boolean "--opt2", help: false
+ @options.separator "other options"
+ @options.boolean "--opt3", help: false
+ @options.boolean "--opt4"
+
+ _usage, help = @options.to_s.squeeze(" ").split("\n", 2)
+ assert_equal "--opt1 \nother options\n --opt4", help.strip
+ end
end
describe "#method_missing" do