Skip to content

Fix sprintf(): Too few arguments when color tokens appear in a sprintf format string#197

Merged
swissspidy merged 3 commits intomainfrom
copilot/fix-sprintf-too-few-arguments
Mar 2, 2026
Merged

Fix sprintf(): Too few arguments when color tokens appear in a sprintf format string#197
swissspidy merged 3 commits intomainfrom
copilot/fix-sprintf-too-few-arguments

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

  • Fix Streams::render() to strip color tokens from format string before sprintf when colorization is disabled
  • Add unit tests that verify render() with color tokens in a format string works correctly when colorization is both disabled and enabled
  • Update inline comment to accurately describe that color tokens are normalized (colorized or stripped) before reaching sprintf
  • Run tests to verify fix (95/95 pass)
Original prompt

This section details on the original issue you should resolve

<issue_title>sprintf(): Too few arguments</issue_title>
<issue_description>Can't really figure this one out. Sometimes (not often, but often enough to end up in my logs and irritate me) \cli\line() causes a warning: sprintf(): Too few arguments.

This is the offending line:

\cli\line("[%C%k%s%N] Starting!", date("Y-m-d H:i:s"));

Can't really see how the argument to sprintf() gets lost here, but obv it does.

environment: php5.6.33 (with mbstring) on debian, php-cli-tools v0.11.8.

Logfile trace:

in function LoggingErrorHandler::handleError
in function call_user_func in /foo/include/classes/LoggingErrorHandler.php on line 402
in function LoggingErrorHandler::{closure}
in function sprintf
in function call_user_func_array in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 48
in function cli\Streams::render
in function call_user_func_array in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 13
in function cli\Streams::_call in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 68
in function cli\Streams::out
in function call_user_func_array in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 13
in function cli\Streams::_call in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 95
in function cli\Streams::line
in function call_user_func_array in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/Streams.php on line 13
in function cli\Streams::_call in /foo/include/deps/wp-cli/php-cli-tools/lib/cli/cli.php on line 62
in function cli\line in /foo/bin/my_foobar_script.php on line 35
```</issue_description>

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

<comments>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…nd colorization disabled

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix sprintf() warning due to too few arguments Fix sprintf(): Too few arguments when color tokens appear in a sprintf format string Mar 2, 2026
@swissspidy swissspidy added this to the 0.12.8 milestone Mar 2, 2026
@swissspidy swissspidy marked this pull request as ready for review March 2, 2026 10:41
Copilot AI review requested due to automatic review settings March 2, 2026 10:41
@swissspidy swissspidy requested a review from a team as a code owner March 2, 2026 10:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a warning in Streams::render() where unprocessed color tokens (e.g. %C, %k, %N) could reach sprintf() when colorization is disabled (non-TTY), causing sprintf(): Too few arguments.

Changes:

  • Strip color tokens from the sprintf format string by calling Colors::decolorize() when colorization is disabled.
  • Add unit tests covering \cli\render() behavior with color tokens in the format string under both disabled and enabled colorization.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/cli/Streams.php Ensures color tokens don’t reach sprintf() in non-color mode by decolorizing the format string.
tests/Test_Cli.php Adds regression tests for render() with color-token-bearing format strings in both color modes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…intf

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy merged commit aff2be3 into main Mar 2, 2026
20 checks passed
@swissspidy swissspidy deleted the copilot/fix-sprintf-too-few-arguments branch March 2, 2026 11:35
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.

sprintf(): Too few arguments

3 participants