Skip to content

Stdlib: getopt() — CLI argv after script name not parsed (cliRequestArgv wiring, php_getopt.c) #9093

Description

@PurHur

Category

stdlib · php-src-strict · spec refresh 2026-06-18

Problem

getopt() is registered (function_exists → true; #3251 / #9065 stale “missing” title). Option parsing works for php bin/vm.php -r '...' (CLI flags after -r script reach getopt), but script-file invocation may still return an empty array when flags follow the script path — unlike Zend SAPI argv layout.

Workers must verify both entry paths; do not close on -r alone.

php-src reference

Repro

Create test/repro/maintainer_getopt_cli.php (referenced in prior spec — not yet committed; implementer should add):

<?php
var_export(getopt('ab:', ['long:', 'opt::']));
echo "\n";

Commands

# Script-file path (primary repro)
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro/maintainer_getopt_cli.php -a -b B --long L --opt=V -- -x'

./script/docker-exec.sh -- bash -lc 'php test/repro/maintainer_getopt_cli.php -a -b B --long L --opt=V -- -x'

# Inline -r path (audit 2026-06-18 — WORKS on VM)
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php -r "var_export(getopt(\"ab:\", [\"long:\", \"opt::\"])); echo PHP_EOL;" -a -b B'
# Actual: array ( "a" => false, "b" => "B", ) ✅
Path Expected VM audit 2026-06-18
-r one-liner parsed opts ✅ non-empty
script.php -a … parsed opts verify / likely empty ❌

Expected Zend shape for full repro:

array ('a' => false, 'b' => 'B', 'long' => 'L', 'opt' => 'V')

Scope (this repo)

Layer Path
VM ext/standard/getopt.php, ext/standard/GetoptEngine.php
Runtime cliRequestArgv population in VM context / bin/vm.php driver — must mirror SAPI argv after script name
JIT lib/JIT/Builtin/GetoptJitHelper.php — same argv snapshot
Tests test/compliance/cases/stdlib/getopt_cli.phpt

Done when

Supersedes stale audit in #9065 (function exists; behavior gap). Ref: #1492, #3251

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions