Skip to content

Commit 66628ed

Browse files
committed
fixing alt-short with :none ordering issue
1 parent 46639a2 commit 66628ed

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/optimist.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,15 @@ def initialize
703703

704704
def add(values)
705705
values = [values] unless values.is_a?(Array) # box the value
706-
values.compact.each do |val|
707-
if val == :none
706+
values = values.compact
707+
if values.include?(:none)
708+
if values.size == 1
708709
@auto = false
709-
raise "Cannot set short to :none if short-chars have been defined '#{@chars}'" unless chars.empty?
710-
next
710+
return
711711
end
712+
raise ArgumentError, "Cannot use :none with any other values in short option: #{values.inspect}"
713+
end
714+
values.each do |val|
712715
strval = val.to_s
713716
sopt = case strval
714717
when /^-(.)$/ then $1

test/optimist/alt_names_test.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_help_string
1818
end
1919

2020
def test_altshort
21-
@p.opt :catarg, "desc", :short => ["c", "-C"]
21+
@p.opt :catarg, "desc", :short => ["c", "-C"]
2222
opts = @p.parse %w(-c)
2323
assert_equal true, opts[:catarg]
2424
opts = @p.parse %w(-C)
@@ -27,6 +27,18 @@ def test_altshort
2727
assert_raises(CommandlineError) { @p.parse %w(-cC) }
2828
end
2929

30+
def test_altshort_invalid_none
31+
assert_raises(ArgumentError) {
32+
@p.opt :something, "some opt", :short => [:s, :none]
33+
}
34+
assert_raises(ArgumentError) {
35+
@p.opt :something, "some opt", :short => [:none, :s]
36+
}
37+
assert_raises(ArgumentError) {
38+
@p.opt :zumthing, "some opt", :short => [:none, :none]
39+
}
40+
end
41+
3042
def test_altshort_with_multi
3143
@p.opt :flag, "desc", :short => ["-c", "C", :x], :multi => true
3244
@p.opt :num, "desc", :short => ["-n", "N"], :multi => true, type: Integer

0 commit comments

Comments
 (0)