Skip to content

Commit 0222fe5

Browse files
committed
Ensure default option/argument is not erroneously aliased
Add test add handler continue loop after rescue remove byebug history spelling
1 parent ff37cba commit 0222fe5

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/thor/parser/arguments.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ def initialize(arguments = [])
3030

3131
arguments.each do |argument|
3232
if !argument.default.nil?
33-
@assigns[argument.human_name] = argument.default
33+
begin
34+
@assigns[argument.human_name] = argument.default.dup
35+
rescue TypeError # Compatibility shim for un-dup-able Fixnum in Ruby < 2.4
36+
@assigns[argument.human_name] = argument.default
37+
next
38+
end
3439
elsif argument.required?
3540
@non_assigned_required << argument
3641
end

spec/parser/options_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,10 @@ def remaining
302302
"Expected '--fruit' to be one of #{enum.join(', ')}; got orange")
303303
end
304304

305-
it "allows multiple values if repeatable is specified" do
306-
create :foo => Thor::Option.new("foo", :type => :string, :repeatable => true)
307-
305+
it "does not erroneously mutate defaults" do
306+
create :foo => Thor::Option.new("foo", :type => :string, :repeatable => true, :required => false, :default => [])
308307
expect(parse("--foo=bar", "--foo", "12")["foo"]).to eq(["bar", "12"])
309-
expect(parse("--foo", "13", "--foo", "14")["foo"]).to eq(["bar", "12", "13", "14"])
308+
expect(@opt.instance_variable_get(:@switches)["--foo"].default).to eq([])
310309
end
311310
end
312311

0 commit comments

Comments
 (0)