Skip to content

Comments

Add support for argument aliases#6176

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

Add support for argument aliases#6176
Copilot wants to merge 16 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
    • Optimized get_arg_aliases() to use single-pass parsing (O(n) instead of O(n²))
    • 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 (restored)
    • 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()
    • Cached alias map computation in invoke() to avoid duplicate parsing
    • 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 (with and without quotes)
    • 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
    • Performance optimization implemented (O(n²) → O(n))
    • Alias caching implemented to avoid duplicate parsing
    • YAML boolean handling restored for edge case
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.83333% 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 97.36% 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

This comment was marked as resolved.

@swissspidy swissspidy added this to the 3.0.0 milestone Feb 6, 2026
gemini-code-assist[bot]

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Refactored get_arg_aliases() to use a single-pass state machine instead of
calling get_param_or_flag_args() for each parameter, which itself iterates
over all doc comment lines. This eliminates the nested loop pattern and
improves performance from O(n²) to O(n) where n is the number of doc lines.

Key changes:
- Parse doc comment in a single pass using state machine
- Track current parameter and YAML block state
- Preserve indentation in YAML blocks for correct parsing
- Process aliases when YAML block ends

This is especially beneficial for commands with many parameters and
extensive documentation, where the nested iteration could cause
noticeable performance issues during command loading.

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

This comment was marked as resolved.

This comment was marked as resolved.

Modified resolve_arg_aliases() to accept pre-computed aliases as a parameter
instead of fetching them internally. In invoke(), compute the alias map once
via get_arg_aliases() and reuse it for both assoc_args and extra_args.

This eliminates duplicate parsing of the PHPDoc and ensures consistent debug
output, improving performance when both argument arrays need resolution.

Also updated test_get_arg_aliases_yaml_boolean_handling to use quoted YAML
values ('n' and 'y' instead of unquoted) to work with the recent removal of
special boolean handling.

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

This comment was marked as resolved.

Re-added the special case handling for YAML boolean values that was
previously removed. This is necessary because YAML 1.1 interprets
single letters 'n', 'N', 'y', 'Y' as booleans (false/true), which
when cast to strings become empty or '1', breaking alias registration.

Without this handling, aliases like `alias: n` fail to work because:
1. YAML parses 'n' as boolean false
2. (string)false becomes ''
3. Empty string is filtered out, no alias registered

The fix converts boolean false back to 'n' and boolean true back to 'y',
allowing common single-letter aliases to work without requiring quotes.

This fixes the failing Behat test for `-n=42` alias usage.

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

This comment was marked as resolved.

@swissspidy
Copy link
Member

Possible alternative syntax: [--with-dependencies|w], [--verbose|v|wordy|deprecated-name]

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