Skip to content

Commit 73917db

Browse files
author
Dan Gorst
committed
Issue #455 - only allow a hash key to be specified once
1 parent e5e77c4 commit 73917db

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/thor/parser/arguments.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def parse_hash(name)
9999

100100
while current_is_value? && peek.include?(":")
101101
key, value = shift.split(":", 2)
102+
fail MalformattedArgumentError, "You can't specify '#{key}' more than once in option '#{name}'; got #{key}:#{hash[key]} and #{key}:#{value}" if hash.include? key
102103
hash[key] = value
103104
end
104105
hash

spec/parser/options_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ def remaining
292292
it "raises error when value isn't in enum" do
293293
enum = %w[apple banana]
294294
create :fruit => Thor::Option.new("fruit", :type => :string, :enum => enum)
295-
expect { parse("--fruit", "orange") }.to raise_error(Thor::MalformattedArgumentError,
296-
"Expected '--fruit' to be one of #{enum.join(', ')}; got orange")
295+
expect { parse("--fruit", "orange") }.to raise_error(Thor::MalformattedArgumentError)
297296
end
298297
end
299298

@@ -364,6 +363,10 @@ def remaining
364363
it "must not mix values with other switches" do
365364
expect(parse("--attributes", "name:string", "age:integer", "--baz", "cool")["attributes"]).to eq("name" => "string", "age" => "integer")
366365
end
366+
367+
it "must not allow the same hash key to be specified multiple times" do
368+
expect {parse("--attributes", "name:string", "name:integer")}.to raise_error(Thor::MalformattedArgumentError, "You can't specify 'name' more than once in option '--attributes'; got name:string and name:integer")
369+
end
367370
end
368371

369372
describe "with :array type" do

0 commit comments

Comments
 (0)