Skip to content

Commit

Permalink
Merge pull request #9313 from AriaXLi/PUP-11693
Browse files Browse the repository at this point in the history
(PUP-11693) Global OptionParser ignores partially matched invalid params
  • Loading branch information
AriaXLi authored Apr 15, 2024
2 parents 3820e24 + c579786 commit ea44ace
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/puppet/util/command_line/trollop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,21 @@ def parse cmdline = ARGV
when /^-([^-])$/
@short[::Regexp.last_match(1)]
when /^--no-([^-]\S*)$/
@long["[no-]#{::Regexp.last_match(1)}"]
possible_match = @long["[no-]#{::Regexp.last_match(1)}"]
if !possible_match
Puppet.warning _("Partial argument match detected: %{arg}. Partial argument matching will be deprecated in Puppet 9.") % { arg: arg }
@long["[no-]#{::Regexp.last_match(1).tr('-', '_')}"] || @long["[no-]#{::Regexp.last_match(1).tr('_', '-')}"]
else
possible_match
end
when /^--([^-]\S*)$/
@long[::Regexp.last_match(1)] || @long["[no-]#{::Regexp.last_match(1)}"]
possible_match = @long[::Regexp.last_match(1)] || @long["[no-]#{::Regexp.last_match(1)}"]
if !possible_match
Puppet.warning _("Partial argument match detected: %{arg}. Partial argument matching will be deprecated in Puppet 9.") % { arg: arg }
@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('_', '-')}"]
else
possible_match
end
else
raise CommandlineError, _("invalid argument syntax: '%{arg}'") % { arg: arg }
end
Expand Down
72 changes: 72 additions & 0 deletions spec/unit/util/command_line_utils/puppet_option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
)
end

it "parses a 'long' option with a value and converts '-' to '_' & warns" do
parses(
:option => ["--an_gry", "Angry", :REQUIRED],
:from_arguments => ["--an-gry", "foo"],
:expects => "foo"
)
expect(@logs).to have_matching_log(/Partial argument match detected: --an-gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "parses a 'long' option with a value and converts '_' to '-' & warns" do
parses(
:option => ["--an-gry", "Angry", :REQUIRED],
:from_arguments => ["--an_gry", "foo"],
:expects => "foo"
)
expect(@logs).to have_matching_log(/Partial argument match detected: --an_gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "parses a 'short' option with a value" do
parses(
:option => ["--angry", "-a", "Angry", :REQUIRED],
Expand All @@ -39,6 +57,24 @@
)
end

it "converts '_' to '-' with a 'long' option & warns" do
parses(
:option => ["--an-gry", "Angry", :NONE],
:from_arguments => ["--an_gry"],
:expects => true
)
expect(@logs).to have_matching_log(/Partial argument match detected: --an_gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "converts '-' to '_' with a 'long' option & warns" do
parses(
:option => ["--an_gry", "Angry", :NONE],
:from_arguments => ["--an-gry"],
:expects => true
)
expect(@logs).to have_matching_log(/Partial argument match detected: --an-gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "parses a 'short' option" do
parses(
:option => ["--angry", "-a", "Angry", :NONE],
Expand All @@ -55,6 +91,42 @@
)
end

it "resolves '-' to '_' with '--no-blah' syntax" do
parses(
:option => ["--[no-]an_gry", "Angry", :NONE],
:from_arguments => ["--no-an-gry"],
:expects => false
)
expect(@logs).to have_matching_log(/Partial argument match detected: --no-an-gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "resolves '_' to '-' with '--no-blah' syntax" do
parses(
:option => ["--[no-]an-gry", "Angry", :NONE],
:from_arguments => ["--no-an_gry"],
:expects => false
)
expect(@logs).to have_matching_log(/Partial argument match detected: --no-an_gry. Partial argument matching will be deprecated in Puppet 9./)
end

it "resolves '-' to '_' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
parses(
:option => ["--[no-]rag-e", "Rage", :NONE],
:from_arguments => ["--rag_e"],
:expects => true
)
expect(@logs).to have_matching_log(/Partial argument match detected: --rag_e. Partial argument matching will be deprecated in Puppet 9./)
end

it "resolves '_' to '-' & warns when option is defined with '--no-blah syntax' but argument is given in '--option' syntax" do
parses(
:option => ["--[no-]rag_e", "Rage", :NONE],
:from_arguments => ["--rag-e"],
:expects => true
)
expect(@logs).to have_matching_log(/Partial argument match detected: --rag-e. Partial argument matching will be deprecated in Puppet 9./)
end

it "overrides a previous argument with a later one" do
parses(
:option => ["--[no-]rage", "Rage", :NONE],
Expand Down

0 comments on commit ea44ace

Please sign in to comment.