Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-11693) Global OptionParser ignores partially matched invalid params #9313

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('_', '-')}"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I left a comment, but it was marked as "pending" so maybe it wasn't visible? In any case, I think there are a couple adjustments needed:

  • The warning is triggered anytime an application-specific option is given:

    $ bundle exec puppet apply --execute "notice(0)"
    Warning: Partial argument match detected: --execute. Partial argument matching will be deprecated in Puppet 9.
    

    I think we only want to trigger the warning if an argument is given that doesn't match a global option, but it does match one of the translated versions. So --execute is not a global option (it's specific to the apply application) and shouldn't trigger the warning. The argument --show-diff is also not a global option, but it does match the translated version --show_diff. So in that case we do want to print the warning.

  • If the warning is triggered, it would be good to specify what option the user should be using, so they can self-correct. For example, if a class expects an argument of type String, but you pass it an Integer, we print both the expected and given types:

    Class[One]: parameter 'a' expects a String value, got Integer
    
  • Could you use Puppet.deprecation_warning instead of Puppet.warning so that the deprecation (along with all other deprecations) can be suppressed using puppet apply --disable_warnings deprecations ...?

  • Technically, partial argument matches are deprecated now (as of this PR) and the functionality will be removed in a future release. For example, this is the message we print for deprecated puppet applications:

    Puppet.deprecation_warning(_("'puppet %{face}' is deprecated and will be removed in a future release") % { face: @face.name })

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