Skip to content

Add --old and --new flags to support strings starting with hyphens#210

Merged
swissspidy merged 14 commits intomainfrom
copilot/fix-search-replace-hyphen-issue
Feb 19, 2026
Merged

Add --old and --new flags to support strings starting with hyphens#210
swissspidy merged 14 commits intomainfrom
copilot/fix-search-replace-hyphen-issue

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

Fix for Search-Replace Command with Hyphenated Words

The issue is that WP-CLI's argument parser treats strings starting with -- as flags/options instead of positional arguments. This prevents users from searching for strings like "--old-word".

Solution

Added two new optional flags --old=<value> and --new=<value> that can be used instead of positional arguments when the search/replacement strings start with --.

The implementation provides flexible usage: you can mix one flag with one positional argument, but cannot use both flags along with positional arguments to avoid confusion.

Changes Made

  1. Added new flags: --old and --new as optional alternatives to positional arguments
  2. Updated argument handling:
    • Checks for flags individually, falling back to positional arguments
    • Prevents confusing scenarios where both flags are used with positional arguments
    • Allows flexible mixing (e.g., --old='value' with positional new)
    • Validates that values are non-empty strings
  3. Improved error messages: Added context-aware error messages explaining workarounds when arguments are missing
  4. Added documentation: Updated docblock with flag descriptions and usage examples
  5. Added comprehensive tests: Created Behat test scenarios covering all edge cases:
    • Double-hyphen strings with flags
    • Missing arguments
    • Only --old flag without --new (errors)
    • Only --new flag without --old (errors)
    • Both flags with positional args (errors)
    • Empty strings via flags (errors)
    • Single-hyphen strings (works with positional args)
    • Flexible mixing of one flag with one positional arg

Example Usage

# Old way (doesn't work for strings starting with --)
wp search-replace "--old-word" "new-word"  # Error: unknown --old-word parameter

# New way (works with the new flags)
wp search-replace --old='--old-word' --new='new-word'

# Traditional usage still works
wp search-replace 'foo' 'bar'

# Flexible mixing is allowed
wp search-replace --old='--problem' 'solution'

# Single hyphens work with positional args
wp search-replace '-word' '-replacement'

Backward Compatibility

The change is fully backward compatible. Users can still use positional arguments as before.

Testing

  • ✅ All linting checks pass
  • ✅ Code style (PHPCS) checks pass
  • ✅ Static analysis (PHPStan) passes
  • ✅ No security vulnerabilities detected
  • ✅ Comprehensive Behat test scenarios added covering all edge cases
Original prompt

This section details on the original issue you should resolve

<issue_title>I can not replace a word with several hyphens in its name</issue_title>
<issue_description>I use: wp search-replace "--old-word" "new word"

Error: unknown --old-word parameter</issue_description>

<agent_instructions>See #150 / https://gist.github.com/danielbachhuber/6c398217cff94e8a23a94b6b23aca3aa for a previous attempt</agent_instructions>

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.

@swissspidy

This comment was marked as resolved.

@codecov

This comment was marked as resolved.

Copilot AI and others added 3 commits January 19, 2026 12:54
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Copilot AI changed the title [WIP] Fix search-replace command error for hyphenated words Add --old and --new flags to support strings starting with hyphens Jan 19, 2026
Copilot AI requested a review from swissspidy January 19, 2026 12:58

This comment was marked as resolved.

Copilot AI and others added 3 commits January 19, 2026 15:50
- Validate that --old and --new flags contain non-empty strings
- Prevent simultaneous use of flags and positional arguments
- Add comprehensive test scenarios for edge cases
- Test coverage for: only --old, only --new, mixed usage, empty strings, single hyphen strings

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Only prevent mixed usage when BOTH flags are provided with positional args
- Allow using one flag with one positional arg (e.g., --old with positional new)
- Simplify logic to check flags individually for fallback

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Correct test for mixed usage to use BOTH flags with positional args
- Add test showing one flag can be mixed with one positional arg
- This demonstrates the flexible approach allows --old='value' with positional new

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy marked this pull request as ready for review January 19, 2026 17:03
@swissspidy swissspidy requested a review from a team as a code owner January 19, 2026 17:03
@swissspidy swissspidy added this to the 2.1.10 milestone Jan 22, 2026
@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

@swissspidy swissspidy merged commit 6b6a2b3 into main Feb 19, 2026
60 checks passed
@swissspidy swissspidy deleted the copilot/fix-search-replace-hyphen-issue branch February 19, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:search-replace Related to 'search-replace' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I can not replace a word with several hyphens in its name

3 participants