Skip to content

Commit 9d30294

Browse files
committed
(PUP-11693) Update behavior when partial argument match detected
This commit updates the Global OptionParser (Trollop) to only warn when the passed in argument partially matches a valid global option. Additionally, the warning is more descriptive now and includes the argument given and the correct global option.
1 parent e9bccae commit 9d30294

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

lib/puppet/util/command_line/trollop.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,22 @@ def parse cmdline = ARGV
337337
when /^--no-([^-]\S*)$/
338338
possible_match = @long["[no-]#{::Regexp.last_match(1)}"]
339339
if !possible_match
340-
Puppet.warning _("Partial argument match detected: %{arg}. Partial argument matching will be deprecated in Puppet 9.") % { arg: arg }
341-
@long["[no-]#{::Regexp.last_match(1).tr('-', '_')}"] || @long["[no-]#{::Regexp.last_match(1).tr('_', '-')}"]
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
342345
else
343346
possible_match
344347
end
345348
when /^--([^-]\S*)$/
346349
possible_match = @long[::Regexp.last_match(1)] || @long["[no-]#{::Regexp.last_match(1)}"]
347350
if !possible_match
348-
Puppet.warning _("Partial argument match detected: %{arg}. Partial argument matching will be deprecated in Puppet 9.") % { arg: arg }
349-
@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('_', '-')}"]
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
350356
else
351357
possible_match
352358
end

spec/unit/util/command_line_utils/puppet_option_parser_spec.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
:from_arguments => ["--angry", "foo"],
1212
:expects => "foo"
1313
)
14+
expect(@logs).to be_empty
1415
end
1516

1617
it "parses a 'long' option with a value and converts '-' to '_' & warns" do
@@ -19,7 +20,7 @@
1920
:from_arguments => ["--an-gry", "foo"],
2021
:expects => "foo"
2122
)
22-
expect(@logs).to have_matching_log(/Partial argument match detected: --an-gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
2324
end
2425

2526
it "parses a 'long' option with a value and converts '_' to '-' & warns" do
@@ -28,7 +29,7 @@
2829
:from_arguments => ["--an_gry", "foo"],
2930
:expects => "foo"
3031
)
31-
expect(@logs).to have_matching_log(/Partial argument match detected: --an_gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
3233
end
3334

3435
it "parses a 'short' option with a value" do
@@ -37,6 +38,7 @@
3738
:from_arguments => ["-a", "foo"],
3839
:expects => "foo"
3940
)
41+
expect(@logs).to be_empty
4042
end
4143

4244
it "overrides a previous argument with a later one" do
@@ -45,6 +47,7 @@
4547
:from_arguments => ["--later", "tomorrow", "--later", "morgen"],
4648
:expects => "morgen"
4749
)
50+
expect(@logs).to be_empty
4851
end
4952
end
5053

@@ -63,7 +66,7 @@
6366
:from_arguments => ["--an_gry"],
6467
:expects => true
6568
)
66-
expect(@logs).to have_matching_log(/Partial argument match detected: --an_gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
6770
end
6871

6972
it "converts '-' to '_' with a 'long' option & warns" do
@@ -72,7 +75,7 @@
7275
:from_arguments => ["--an-gry"],
7376
:expects => true
7477
)
75-
expect(@logs).to have_matching_log(/Partial argument match detected: --an-gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
7679
end
7780

7881
it "parses a 'short' option" do
@@ -89,6 +92,7 @@
8992
:from_arguments => ["--no-rage"],
9093
:expects => false
9194
)
95+
expect(@logs).to be_empty
9296
end
9397

9498
it "resolves '-' to '_' with '--no-blah' syntax" do
@@ -97,7 +101,7 @@
97101
:from_arguments => ["--no-an-gry"],
98102
:expects => false
99103
)
100-
expect(@logs).to have_matching_log(/Partial argument match detected: --no-an-gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
101105
end
102106

103107
it "resolves '_' to '-' with '--no-blah' syntax" do
@@ -106,7 +110,7 @@
106110
:from_arguments => ["--no-an_gry"],
107111
:expects => false
108112
)
109-
expect(@logs).to have_matching_log(/Partial argument match detected: --no-an_gry. Partial argument matching will be deprecated in Puppet 9./)
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./)
110114
end
111115

112116
it "resolves '-' to '_' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
@@ -115,7 +119,7 @@
115119
:from_arguments => ["--rag_e"],
116120
:expects => true
117121
)
118-
expect(@logs).to have_matching_log(/Partial argument match detected: --rag_e. Partial argument matching will be deprecated in Puppet 9./)
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./)
119123
end
120124

121125
it "resolves '_' to '-' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
@@ -124,7 +128,7 @@
124128
:from_arguments => ["--rag-e"],
125129
:expects => true
126130
)
127-
expect(@logs).to have_matching_log(/Partial argument match detected: --rag-e. Partial argument matching will be deprecated in Puppet 9./)
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./)
128132
end
129133

130134
it "overrides a previous argument with a later one" do
@@ -133,6 +137,7 @@
133137
:from_arguments => ["--rage", "--no-rage"],
134138
:expects => false
135139
)
140+
expect(@logs).to be_empty
136141
end
137142
end
138143

0 commit comments

Comments
 (0)