Skip to content

Commit 4213ca7

Browse files
committed
Avoid extra spaces from nil/false switches in Options.to_switches
1 parent 4b95e2e commit 4213ca7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/thor/parser/options.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ def self.to_switches(options)
1818
when Hash
1919
"--#{key} #{value.map { |k, v| "#{k}:#{v}" }.join(' ')}"
2020
when nil, false
21-
""
21+
nil
2222
else
2323
"--#{key} #{value.inspect}"
2424
end
25-
end.join(" ")
25+
end.compact.join(" ")
2626
end
2727

2828
# Takes a hash of Thor::Option and a hash with defaults.

spec/parser/options_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def remaining
3535
expect(Thor::Options.to_switches(:color => false)).to eq("")
3636
end
3737

38+
it "avoids extra spaces" do
39+
expect(Thor::Options.to_switches(:color => false, :foo => nil)).to eq("")
40+
end
41+
3842
it "writes --name value for anything else" do
3943
expect(Thor::Options.to_switches(:format => "specdoc")).to eq('--format "specdoc"')
4044
end

0 commit comments

Comments
 (0)