Skip to content

Warn when commands override global arguments#6174

Open
Copilot wants to merge 10 commits intomainfrom
copilot/warn-on-global-argument-override
Open

Warn when commands override global arguments#6174
Copilot wants to merge 10 commits intomainfrom
copilot/warn-on-global-argument-override

Conversation

Copy link
Contributor

Copilot AI commented Dec 20, 2025

Custom commands can define arguments with the same names as WP-CLI's global arguments (--debug, --user, --quiet, etc.), causing flags to silently fail or behave unexpectedly. This makes it difficult to introduce new global arguments without breaking existing commands.

Changes

  • Added check_global_arg_conflicts() validation in multiple locations:

    • In WP_CLI::add_command() for commands added via the public API
    • In CommandFactory::create_subcommand() to catch class-based command methods
    • Parses command synopsis to extract argument names (assoc/flag types)
    • Compares against global arguments from config spec
    • Caches global argument list for performance
    • Issues warning for each conflict detected
  • Added @skipglobalargcheck annotation to allow opt-out:

    • Commands can add @skipglobalargcheck to their PHPdoc to silence warnings
    • Useful for built-in commands that intentionally override global arguments
    • Uses regex pattern matching to avoid false positives
  • Added Behat test scenarios covering:

    • Single conflicts
    • Multiple conflicts
    • No false positives
    • Class-based command method conflicts
    • Skip annotation functionality

Example

Commands registering conflicting arguments now emit warnings at registration time:

Function-based command (with warning):

/**
 * @synopsis [--debug]
 */
WP_CLI::add_command( 'mycommand', function() {} );

// Output:
// Warning: The `mycommand` command is registering an argument '--debug' that conflicts 
// with a global argument of the same name.

Function-based command (warning suppressed):

/**
 * @synopsis [--debug]
 * @skipglobalargcheck
 */
WP_CLI::add_command( 'mycommand', function() {} );

// No warning - intentional override

Class-based command:

class MyCommand {
    /**
     * @synopsis [--debug]
     */
    public function my_method( $args, $assoc_args ) {}
}
WP_CLI::add_command( 'mycommand', 'MyCommand' );

// Output:
// Warning: The `mycommand my_method` command is registering an argument '--debug' that 
// conflicts with a global argument of the same name.

Warnings are informational only—commands continue to work. The --quiet flag suppresses warnings as expected.

Original prompt

This section details on the original issue you should resolve

<issue_title>Warn if overriding global argument</issue_title>
<issue_description>## Feature Request

Describe your use case and the problem you are facing

Right now, it's possible for custom commands to override global arguments such as debug or user, either accidentally or willingly.

This can make for confusing user experience because flags won't work as expected.

Plus, it makes it difficult to introduce new global arguments in the future because you won't know about it.

Describe the solution you'd like

If a command defines an argument with the same name as an existing global argument in WP-CLI, there should be a warning either when registering that command or when running the command.

Stretch goal:

This functionality could be used to more easily introduce new global arguments as well, by warning if an argument clashes with one we intend to introduce in the future. For example, in v3.0.0 we could warn you about using argument "foo", but only actually introduce it in v3.1.0, where it would error instead.
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Dec 20, 2025
Copilot AI and others added 3 commits December 20, 2025 19:50
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
… generic type

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add warning for overriding global arguments in commands Warn when commands override global arguments Dec 20, 2025
Copilot AI requested a review from swissspidy December 20, 2025 19:56
@codecov
Copy link

codecov bot commented Dec 20, 2025

Codecov Report

❌ Patch coverage is 68.96552% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/class-wp-cli.php 68.96% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review December 20, 2025 20:23
@swissspidy swissspidy requested a review from a team as a code owner December 20, 2025 20:23
@swissspidy swissspidy added this to the 3.0.0 milestone Jan 20, 2026
@swissspidy swissspidy requested a review from Copilot January 20, 2026 15:02

This comment was marked as resolved.

Check for global argument conflicts in CommandFactory::create_subcommand() to catch conflicts in class-based commands that bypass WP_CLI::add_command()

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Allows commands to opt-out of global argument conflict warnings by adding @skipglobalargcheck to their PHPdoc. This is useful for built-in commands that intentionally override global arguments.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

- Remove 'This can lead to unexpected behavior' from warning to match test expectations
- Use regex pattern matching for @skipglobalargcheck to prevent false positives
- Ensures annotation is only detected as an actual PHPDoc tag, not in description text

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn if overriding global argument

2 participants