Skip to content

Fix --config option when placed before the command name#46

Merged
TomasVotruba merged 2 commits into
mainfrom
fix-config-option-before-command
Jul 11, 2026
Merged

Fix --config option when placed before the command name#46
TomasVotruba merged 2 commits into
mainfrom
fix-config-option-before-command

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Problem

The --config option failed when placed before the command name (or when running the default command with no command name):

ecs --config=ecs.php
# Run failed: Unknown option: "--config=ecs.php"

ecs check --config=ecs.php (option after the command) worked, hence it looked like --config was gone after the symfony/console → entropy migration.

Reported in #27 (comment).

Root cause

The Entropy input parser force-parses the first argv token as a boolean flag:

$command = array_shift($argv);
if (str_starts_with($command, '-')) {
    $options[ltrim($command, '-')] = true;  // "--config=ecs.php" → bogus flag "config=ecs.php"
    $command = null;
}

Only the boolean-flag case is handled; a leading --long=value / --long value is mangled. The parser's main loop handles them correctly, so options after the command name always worked.

Fix

In ecs_normalize_argv() (already the layer that adapts argv for Entropy), inject the default check command when the first real argument is an option, so the working loop parses it. Help flags stay untouched so the global --help listing is preserved.

+    // $normalized[0] is the script name; the first real argument sits at index 1.
+    // Keep "-h"/"--help" untouched, so it still prints the global command list.
+    $helpFlags = ['-h', '--help'];
+    if (isset($normalized[1]) && str_starts_with($normalized[1], '-') && ! in_array($normalized[1], $helpFlags, true)) {
+        array_splice($normalized, 1, 0, 'check');
+    }

Before / after

# before
$ ecs --config=ecs.php
Run failed: Unknown option: "--config=ecs.php"

# after
$ ecs --config=ecs.php
# loads ecs.php and checks normally

Test

New config_option.yaml CI workflow runs the real binary with --config in every position (--config=x, --config x, check --config=x) and asserts the custom config loads and no Unknown option is printed.

The Entropy input parser mishandles a leading long option: the first argv
token is force-parsed as a boolean flag, so "ecs --config=ecs.php" turned
into a bogus "config=ecs.php" flag and failed with 'Unknown option'.
Options after the command name ("ecs check --config=ecs.php") worked.

Inject the default "check" command in ecs_normalize_argv() when the first
real argument is an option, so the parser's main loop handles it correctly.
Help flags stay untouched to keep the global "--help" listing.

Adds a config_option.yaml CI workflow that runs the real binary with
--config in every position.
CI installs the latest deps (composer.lock is not committed), so it runs
rector 2.5.5, which flags AbstractDocBlockFixer with early-return rules.
Bump the constraint to ^2.5 and apply the required refactor, so the Rector
job passes.
@TomasVotruba TomasVotruba merged commit 4573b55 into main Jul 11, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the fix-config-option-before-command branch July 11, 2026 07:45
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.

1 participant