8000 Don't treat empty string as an option description · ruby/optparse@078638e · GitHub
[go: up one dir, main page]

Skip to content

Commit 078638e

Browse files
authored
Don't treat empty string as an option description
1 parent 3bcca8a commit 078638e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/optparse.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ def make_switch(opts, block = nil)
15021502
style = notwice(default_style.guess(arg = o), style, 'style')
15031503
default_pattern, conv = search(:atype, Object) unless default_pattern
15041504
else
1505-
desc.push(o)
1505+
desc.push(o) if o && !o.empty?
15061506
end
15071507
end
15081508

test/optparse/test_summary.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,27 @@ def test_ver
5555
o.release = "rel"
5656
assert_equal "foo 0.1 (rel)", o.ver
5757
end
58+
59+
# https://github.com/ruby/optparse/issues/37
60+
def test_very_long_without_short
61+
o = OptionParser.new do |opts|
62+
# This causes TypeError
63+
opts.on('', '--long-long-option-param-without-short', "Error desc") { options[:long_long_option_param_without_short] = true }
64+
opts.on('', '--long-option-param', "Long desc") { options[:long_option_param_without_short] = true }
65+
opts.on('-a', '--long-long-option-param-with-short', "Normal description") { options[:long_long_option_param_with_short] = true }
66+
67+
opts.on('', '--long-long-option-param-without-short-but-with-desc', 'Description of the long long param') { options[:long_long_option_param_without_short_but_with_desc] = true }
68+
end
69+
70+
s = o.summarize
71+
72+
assert_match(/^\s*--long-long-option-param-without-short$/, s[0])
73+
assert_match(/^\s*Error desc$/, s[1])
74+
assert_match(/^\s*--long-option-param\s+Long desc$/, s[2])
75+
assert_match(/^\s*-a\s+Normal description$/, s[3])
76+
assert_match(/^\s*--long-long-option-param-with-short$/, s[4])
77+
78+
assert_match(/^\s*--long-long-option-param-without-short-but-with-desc$/, s[5])
79+
assert_match(/^\s*Description of the long long param$/, s[6])
80+
end
5881
end

0 commit comments

Comments
 (0)
0