Skip to content

Commit 0a0ab55

Browse files
silugclaude
andcommitted
Remove partial CLI argument matching
Partial argument matching in the CLI option parser has been removed. Arguments must now be specified exactly. Closes #334 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Steven Pritchard <steven.pritchard@gmail.com>
1 parent 954b16d commit 0a0ab55

2 files changed

Lines changed: 28 additions & 78 deletions

File tree

lib/puppet/util/command_line/trollop.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,27 +335,9 @@ def parse cmdline = ARGV
335335
when /^-([^-])$/
336336
@short[::Regexp.last_match(1)]
337337
when /^--no-([^-]\S*)$/
338-
possible_match = @long["[no-]#{::Regexp.last_match(1)}"]
339-
if !possible_match
340-
partial_match = @long["[no-]#{::Regexp.last_match(1).tr('-', '_')}"] || @long["[no-]#{::Regexp.last_match(1).tr('_', '-')}"]
341-
if partial_match
342-
Puppet.deprecation_warning _("Partial argument match detected: correct argument is %{partial_match}, got %{arg}. Partial argument matching is deprecated and will be removed in a future release.") % { arg: arg, partial_match: partial_match }
343-
end
344-
partial_match
345-
else
346-
possible_match
347-
end
338+
@long["[no-]#{::Regexp.last_match(1)}"]
348339
when /^--([^-]\S*)$/
349-
possible_match = @long[::Regexp.last_match(1)] || @long["[no-]#{::Regexp.last_match(1)}"]
350-
if !possible_match
351-
partial_match = @long[::Regexp.last_match(1).tr('-', '_')] || @long[::Regexp.last_match(1).tr('_', '-')] || @long["[no-]#{::Regexp.last_match(1).tr('-', '_')}"] || @long["[no-]#{::Regexp.last_match(1).tr('_', '-')}"]
352-
if partial_match
353-
Puppet.deprecation_warning _("Partial argument match detected: correct argument is %{partial_match}, got %{arg}. Partial argument matching is deprecated and will be removed in a future release.") % { arg: arg, partial_match: partial_match }
354-
end
355-
partial_match
356-
else
357-
possible_match
358-
end
340+
@long[::Regexp.last_match(1)] || @long["[no-]#{::Regexp.last_match(1)}"]
359341
else
360342
raise CommandlineError, _("invalid argument syntax: '%{arg}'") % { arg: arg }
361343
end

spec/unit/util/command_line_utils/puppet_option_parser_spec.rb

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@
1414
expect(@logs).to be_empty
1515
end
1616

17-
it "parses a 'long' option with a value and converts '-' to '_' & warns" do
18-
parses(
19-
:option => ["--an_gry", "Angry", :REQUIRED],
20-
:from_arguments => ["--an-gry", "foo"],
21-
:expects => "foo"
22-
)
23-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --an_gry, got --an-gry. Partial argument matching is deprecated and will be removed in a future release./)
17+
it "raises for a 'long' option where '-' and '_' don't match exactly" do
18+
option_parser.on("--an_gry", "Angry", :REQUIRED) { |val| }
19+
expect { option_parser.parse(["--an-gry", "foo"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
2420
end
2521

26-
it "parses a 'long' option with a value and converts '_' to '-' & warns" do
27-
parses(
28-
:option => ["--an-gry", "Angry", :REQUIRED],
29-
:from_arguments => ["--an_gry", "foo"],
30-
:expects => "foo"
31-
)
32-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --an-gry, got --an_gry. Partial argument matching is deprecated and will be removed in a future release./)
22+
it "raises for a 'long' option where '_' and '-' don't match exactly" do
23+
option_parser.on("--an-gry", "Angry", :REQUIRED) { |val| }
24+
expect { option_parser.parse(["--an_gry", "foo"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
3325
end
3426

3527
it "parses a 'short' option with a value" do
@@ -60,22 +52,14 @@
6052
)
6153
end
6254

63-
it "converts '_' to '-' with a 'long' option & warns" do
64-
parses(
65-
:option => ["--an-gry", "Angry", :NONE],
66-
:from_arguments => ["--an_gry"],
67-
:expects => true
68-
)
69-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --an-gry, got --an_gry. Partial argument matching is deprecated and will be removed in a future release./)
55+
it "raises for a 'long' option where '_' and '-' don't match exactly" do
56+
option_parser.on("--an-gry", "Angry", :NONE) { |val| }
57+
expect { option_parser.parse(["--an_gry"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
7058
end
7159

72-
it "converts '-' to '_' with a 'long' option & warns" do
73-
parses(
74-
:option => ["--an_gry", "Angry", :NONE],
75-
:from_arguments => ["--an-gry"],
76-
:expects => true
77-
)
78-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --an_gry, got --an-gry. Partial argument matching is deprecated and will be removed in a future release./)
60+
it "raises for a 'long' option where '-' and '_' don't match exactly" do
61+
option_parser.on("--an_gry", "Angry", :NONE) { |val| }
62+
expect { option_parser.parse(["--an-gry"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
7963
end
8064

8165
it "parses a 'short' option" do
@@ -95,41 +79,25 @@
9579
expect(@logs).to be_empty
9680
end
9781

98-
it "resolves '-' to '_' with '--no-blah' syntax" do
99-
parses(
100-
:option => ["--[no-]an_gry", "Angry", :NONE],
101-
:from_arguments => ["--no-an-gry"],
102-
:expects => false
103-
)
104-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --\[no-\]an_gry, got --no-an-gry. Partial argument matching is deprecated and will be removed in a future release./)
82+
it "raises for '--no-blah' syntax where '-' and '_' don't match exactly" do
83+
option_parser.on("--[no-]an_gry", "Angry", :NONE) { |val| }
84+
expect { option_parser.parse(["--no-an-gry"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
10585
end
10686

107-
it "resolves '_' to '-' with '--no-blah' syntax" do
108-
parses(
109-
:option => ["--[no-]an-gry", "Angry", :NONE],
110-
:from_arguments => ["--no-an_gry"],
111-
:expects => false
112-
)
113-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --\[no-\]an-gry, got --no-an_gry. Partial argument matching is deprecated and will be removed in a future release./)
87+
it "raises for '--no-blah' syntax where '_' and '-' don't match exactly" do
88+
option_parser.on("--[no-]an-gry", "Angry", :NONE) { |val| }
89+
expect { option_parser.parse(["--no-an_gry"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
11490
end
11591

116-
it "resolves '-' to '_' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
117-
parses(
118-
:option => ["--[no-]rag-e", "Rage", :NONE],
119-
:from_arguments => ["--rag_e"],
120-
:expects => true
121-
)
122-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --\[no-\]rag-e, got --rag_e. Partial argument matching is deprecated and will be removed in a future release./)
123-
end
92+
it "raises when option is defined with '--no-blah' syntax but '-' and '_' don't match exactly" do
93+
option_parser.on("--[no-]rag-e", "Rage", :NONE) { |val| }
94+
expect { option_parser.parse(["--rag_e"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
95+
end
12496

125-
it "resolves '_' to '-' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
126-
parses(
127-
:option => ["--[no-]rag_e", "Rage", :NONE],
128-
:from_arguments => ["--rag-e"],
129-
:expects => true
130-
)
131-
expect(@logs).to have_matching_log(/Partial argument match detected: correct argument is --\[no-\]rag_e, got --rag-e. Partial argument matching is deprecated and will be removed in a future release./)
132-
end
97+
it "raises when option is defined with '--no-blah' syntax but '_' and '-' don't match exactly" do
98+
option_parser.on("--[no-]rag_e", "Rage", :NONE) { |val| }
99+
expect { option_parser.parse(["--rag-e"]) }.to raise_error(Puppet::Util::CommandLine::PuppetOptionError)
100+
end
133101

134102
it "overrides a previous argument with a later one" do
135103
parses(

0 commit comments

Comments
 (0)