Skip to content

Commit

Permalink
fixing alt-short with :none ordering issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nanobowers committed May 11, 2024
1 parent 46639a2 commit 66628ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,15 @@ def initialize

def add(values)
values = [values] unless values.is_a?(Array) # box the value
values.compact.each do |val|
if val == :none
values = values.compact
if values.include?(:none)
if values.size == 1
@auto = false
raise "Cannot set short to :none if short-chars have been defined '#{@chars}'" unless chars.empty?
next
return
end
raise ArgumentError, "Cannot use :none with any other values in short option: #{values.inspect}"
end
values.each do |val|
strval = val.to_s
sopt = case strval
when /^-(.)$/ then $1
Expand Down
14 changes: 13 additions & 1 deletion test/optimist/alt_names_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_help_string
end

def test_altshort
@p.opt :catarg, "desc", :short => ["c", "-C"]
@p.opt :catarg, "desc", :short => ["c", "-C"]
opts = @p.parse %w(-c)
assert_equal true, opts[:catarg]
opts = @p.parse %w(-C)
Expand All @@ -27,6 +27,18 @@ def test_altshort
assert_raises(CommandlineError) { @p.parse %w(-cC) }
end

def test_altshort_invalid_none
assert_raises(ArgumentError) {
@p.opt :something, "some opt", :short => [:s, :none]
}
assert_raises(ArgumentError) {
@p.opt :something, "some opt", :short => [:none, :s]
}
assert_raises(ArgumentError) {
@p.opt :zumthing, "some opt", :short => [:none, :none]
}
end

def test_altshort_with_multi
@p.opt :flag, "desc", :short => ["-c", "C", :x], :multi => true
@p.opt :num, "desc", :short => ["-n", "N"], :multi => true, type: Integer
Expand Down

0 comments on commit 66628ed

Please sign in to comment.