Skip to content

Stdlib: getopt() — CLI short/long option parsing (ext/standard/getopt.c parity) #3251

Description

@PurHur

Category

stdlib

Problem

CLI tools and bootstrap scripts parse argv with getopt(). Compiled CLI binaries and self-host probes need getopt() in the VM stdlib for Zend parity.

Duplicate: #4473 tracks the same builtin with extended repro (optional-arg ::, repeated long opts). Claim only one of #3251 / #4473 per worker run; close the other when green.

php-src reference

  • ext/standard/php_getopt.c — php_getopt(), PHP_FUNCTION(getopt)
  • ext/standard/php_getopt.h
  • Tests: ext/standard/tests/general_functions/getopt*.phpt

Repro (failure today)

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "var_export(function_exists(\"getopt\"));"'
Runtime function_exists('getopt')
Zend PHP 8.x true
bin/vm.php false

Full behavioral repro (save repro_getopt.php):

<?php
$argv = ['tool.php', '-a', '-b', 'BVAL', '--help', '--output=out.txt', '--output', 'out2.txt'];
$_SERVER['argv'] = $argv;
$_SERVER['argc'] = count($argv);
var_export(getopt('ab:', ['help', 'output:']));
echo "\n";

Zend returns:

array (
  'a' => false,
  'b' => 'BVAL',
  'help' => false,
  'output' => array (0 => 'out.txt', 1 => 'out2.txt'),
)

Scope (this repo)

Layer Files Notes
VM ext/standard/getopt.php (new) Read $argv from VM superglobal / $_SERVER['argv']
Parser Port subset of php_getopt() from php-src or wrap libc getopt_long where harness-safe
Register ext/standard/Module.php
v1 Short abc: / abc::, long ["foo", "bar:"] Match php-src arity rules
Tests test/compliance/cases/stdlib/getopt.phpt
JIT/AOT v1 VM-only acceptable Document in matrix

Done when

  • function_exists('getopt') is true on VM
  • Full repro var_export matches Zend (including repeated --output)
  • Invalid option string → false + E_WARNING like Zend (spot-check)
  • ./script/ci-fast.sh --filter getopt green
  • Capability matrix row added via script/capability-matrix.php

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter getopt'

Links

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 machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions