Skip to content

Add support for argument aliases#6176

Open
Copilot wants to merge 10 commits intomainfrom
copilot/add-argument-aliases-support
Open

Add support for argument aliases#6176
Copilot wants to merge 10 commits intomainfrom
copilot/add-argument-aliases-support

Conversation

Copy link
Contributor

Copilot AI commented Dec 20, 2025

Implementation Plan for Argument Aliases

  • Understand the existing codebase structure for argument handling

    • Reviewed Subcommand.php - handles command invocation and argument validation
    • Reviewed SynopsisParser.php - parses synopsis tokens into structured specs
    • Reviewed SynopsisValidator.php - validates arguments against synopsis
    • Reviewed DocParser.php - extracts command metadata from PHPdoc
    • Reviewed Configurator.php - handles argument parsing from CLI
  • Extend DocParser to support parsing argument aliases from PHPdoc

    • Added get_arg_aliases() method to parse alias metadata for parameters
    • Added get_param_or_flag_args() helper method for both flags and assoc params
    • Stores aliases in a structured format (alias => canonical_name)
    • Fixed YAML boolean parsing issue for single-letter aliases
    • Improved documentation for YAML boolean handling
  • Implement alias resolution logic

    • Added resolve_arg_aliases() method in Subcommand to resolve aliases to canonical argument names
    • Applied alias resolution before argument validation in invoke()
    • Ensured resolved arguments use canonical names in $assoc_args
    • Canonical name takes precedence when both provided
    • Store original docparser to access YAML metadata correctly
    • Simplified logic with two-pass approach per code review
  • Handle short-form aliases (single character with -)

    • Updated Configurator.extract_assoc to recognize single-dash short args
    • Maps short args like -w, -n=5 to full argument names
    • Documented single-letter limitation following CLI conventions
  • Add comprehensive tests

    • Created unit tests for DocParser.get_arg_aliases()
    • Updated test naming to use snake_case convention
    • Added test for YAML boolean handling
    • Created Behat feature tests for argument aliases
    • Tested long-form aliases (--with-dependencies -> -w)
    • Tested short-form aliases (-w -> --with-dependencies)
    • Tested multiple aliases for same argument
    • Tested that $assoc_args uses canonical names
    • All tests passing
    • Manual testing verified functionality works correctly
  • Run all tests and fix any issues

    • Unit tests passing (composer phpunit)
    • Behat test failures fixed
    • Linting passed (composer lint)
    • Code style passed (composer phpcs)
    • Static analysis passed (composer phpstan)
    • Code review feedback addressed
    • CI test failure resolved
    • Test naming convention feedback addressed
Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for argument aliases</issue_title>
<issue_description>## Feature Request

Describe your use case and the problem you are facing

For most CLI tools it's common to have short and long form argument names that can be used interchangeably.

For example, npm install --save-dev is the same as npm install -D (or even npm i -D). composer update --with-dependencies is the same as composer update -w.

WP-CLI doesn't have that.

Describe the solution you'd like

Similar to command aliases, it would be nice to define aliases for arguments as well, both local and global. They could be used to define such shorter versions, but also for renaming deprecated arguments (like the deprecated --blog global argument).

An argument should be able to have multiple aliases.

If an --with-dependencies argument has an alias -w and I run wp <mycommand> -w, the $assoc_args array should still use the with-dependencies array key, so there should be no code change needed to accommodate the aliased version.


Related:

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 20, 2025 20:09
- Extended DocParser to parse 'alias' metadata from PHPdoc parameters
- Updated Subcommand to resolve aliases to canonical names before validation
- Modified Configurator to support single-dash short arguments (e.g., -w, -n=5)
- Added comprehensive unit tests for alias resolution
- Added Behat feature tests for argument aliases

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Store the original DocParser instance in Subcommand to access YAML metadata
- Fix access level visibility issue with $synopsis property
- Add debug logging for alias resolution
- Verify aliases work correctly with manual testing

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

codecov bot commented Dec 20, 2025

Codecov Report

❌ Patch coverage is 95.08197% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/Configurator.php 50.00% 2 Missing ⚠️
php/WP_CLI/DocParser.php 96.42% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

- Simplify alias resolution logic with two-pass approach
- Add documentation for single-letter short argument limitation
- Improve code clarity and maintainability

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for argument aliases in WP-CLI Add support for argument aliases Dec 20, 2025
Copilot AI requested a review from swissspidy December 20, 2025 20:21
@swissspidy

This comment was marked as resolved.

YAML parsers interpret 'n', 'N', 'y', 'Y' as boolean values (false/true).
This caused single-letter aliases like 'n' to be parsed as false and then
converted to empty strings. Added special handling to convert boolean false
back to 'n' and boolean true to 'y', along with proper type conversion and
empty string filtering.

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

This comment was marked as resolved.

@swissspidy swissspidy marked this pull request as ready for review December 20, 2025 21:51
@swissspidy swissspidy requested a review from a team as a code owner December 20, 2025 21:51
@github-actions github-actions bot added command:cli Related to 'cli' command command:cli-alias Related to 'cli alias' command command:cli-cache Related to 'cli cache' command command:config labels Dec 22, 2025
@swissspidy swissspidy requested a review from Copilot January 21, 2026 18:58
@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

This comment was marked as resolved.

…tation

- Renamed all test methods in ArgAliasTest.php from camelCase to snake_case
  to match existing test conventions in the codebase
- Enhanced documentation for YAML boolean value handling to explain the
  behavior more clearly, including the note that 'n'/'N' become 'n' and
  'y'/'Y' become 'y'
- Added new test case for YAML boolean handling to verify correct behavior
- Mentioned that users can quote aliases in YAML to avoid boolean interpretation

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

/gemini review

@swissspidy swissspidy added this to the 3.0.0 milestone Feb 6, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for argument aliases, a valuable feature for command-line tools. The changes span across argument parsing, doc comment parsing, and command dispatching, and are accompanied by a comprehensive set of new unit and feature tests. The implementation is logical and covers various use cases for aliases. I have one suggestion regarding a potential performance issue in DocParser.php where parsing aliases could be made more efficient. Other than that, the changes look good.

Comment on lines +185 to +238
public function get_arg_aliases() {
$aliases = [];
$bits = explode( "\n", $this->doc_comment );

foreach ( $bits as $bit ) {
// Skip empty lines and separator lines
$bit = trim( $bit );
if ( empty( $bit ) || '---' === $bit ) {
continue;
}

// Match parameter definitions:
// - [--param=<value>]
// - --param=<value>
// - [--param]
// - --param
if ( preg_match( '/^\[?--([a-z-_0-9]+)/', $bit, $matches ) ) {
$param_name = $matches[1];
$param_args = $this->get_param_or_flag_args( $param_name );

if ( $param_args && isset( $param_args['alias'] ) ) {
$param_aliases = (array) $param_args['alias'];
foreach ( $param_aliases as $alias ) {
// Handle YAML boolean values (YAML 1.1 spec interprets certain
// values as booleans: y/Y/yes/YES/n/N/no/NO/true/false/on/off)
// For single-letter aliases, we preserve the intent by converting
// boolean values back to their likely string representations.
// Note: This means 'n' and 'N' both become 'n', and 'y' and 'Y' become 'y'.
// Users can avoid this by quoting the alias in YAML: alias: 'N'
if ( false === $alias ) {
$alias = 'n';
} elseif ( true === $alias ) {
$alias = 'y';
}

// Convert to string if not already
$alias = (string) $alias;

// Remove leading dashes if present
$alias = ltrim( $alias, '-' );

// Skip empty aliases
if ( '' === $alias ) {
continue;
}

$aliases[ $alias ] = $param_name;
}
}
}
}

return $aliases;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The implementation of get_arg_aliases() has a potential performance issue. It iterates over each line of the doc comment, and for each line that appears to be a parameter, it calls get_param_or_flag_args(). This helper method in turn calls get_arg_or_param_args(), which also iterates over all lines of the doc comment from the beginning. This results in a nested loop over the doc comment lines, leading to O(n²) complexity where n is the number of lines. For commands with many parameters and extensive documentation, this could introduce a performance bottleneck during command loading.

A more efficient approach would be to parse the doc comment in a single pass, possibly using a state machine to identify parameter definitions and their associated YAML blocks. This would avoid the nested iteration and improve performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:cli Related to 'cli' command command:cli-alias Related to 'cli alias' command command:cli-cache Related to 'cli cache' command command:config

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for argument aliases

2 participants