Skip to content

Fix macro resolution when an escaped argument follows a normal one - #1860

Merged
tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/macro-escaped-arg-after-normal
Dec 3, 2026
Merged

tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/macro-escaped-arg-after-normal

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

When a macro is resolved, each argument placeholder is replaced by the argument given to the macro. Cmd._resolve_macro did this by searching the partly resolved string for the placeholder text (resolved.rsplit(to_replace, maxsplit=1)), starting from the last one. The macro stores where each placeholder starts (macro_arg.start_index), but that index was not used. When the text that was already substituted, or an escaped placeholder ({{1}}, which becomes a literal {1}), looks like another placeholder, the search finds the wrong occurrence and replaces that one.

Real result of the new test: macro create fake {1} "{{1}}" (a normal argument followed by an escaped one), then fake help. The macro should run help {1} with a literal {1}; on main and with this change:

main:   {1} is not a recognized command, alias, or macro.     (the wrong occurrence was replaced, so the command itself became "{1}")
after:  No help on {1}                                        (help was run with the literal {1})

The fix replaces the text at the recorded start_index (resolved[:start_index] + replacement + resolved[end_index:]), instead of searching for it.

Test: test_macro_escaped_arg_after_normal_arg in tests/test_cmd2.py; it fails on main and passes now. The full suite passes (3228 passed, 5 skipped), and ruff check and ruff format --check are clean on the two changed files.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (3a5a2ff) to head (2a9ca6f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1860   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6777           
=======================================
  Hits         6761     6761           
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt tleonhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix and the regression test, @mokshupadhyay. I reproduced the bug on the base commit (3a5a2ff). After macro create fake {1} "{{1}}", running fake help tried to execute a command named {1}. On this PR (2a9ca6f), it runs help with the literal argument {1}, as documented.

Using the recorded positions fixes the underlying problem. The resolver already works from right to left, so each remaining position still points to its original placeholder even when a replacement changes length. I also checked macro create probe {1} {2} followed by probe help "{1}". That reproduced the same bug on the base commit and worked on this branch.

The new test exercises the reported case through the command interface. My local make test run finished with 3,230 passed and 3 skipped. Looks good to merge as written.

@tleonhardt
tleonhardt merged commit 1c55575 into python-cmd2:main Dec 3, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants