php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65673 getopt: cannot report bad options or how much of argv used
Submitted: 2013-09-14 11:34 UTC Modified: -
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: addw at phcomp dot co dot uk Assigned:
Status: Open Package: CGI/CLI related
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-09-14 11:34 UTC] addw at phcomp dot co dot uk
Description:
------------
Getopt has 2 deficiencies that make it hard to use at the command line:

1) If an illegal option is used on the command line, this is not reported to the PHP script. This means that an error/... message cannot be generated.

2) Most programs will expect arguments after the options. getopt() does not let the application know how many elements in argv have been used by options. The Unix getopt(3) will put this number in the global variable 'optind'.

Both of these could be remedied by there being 2 extra elements where the keys are:
'optind' -- number of elements used from argv;
'badopts' -- an array that contains the options that were not recognised.
Since this might confuse existing programs I suggest that these 2 extra elements only by created if the getopt $options argument string starts with a ':'.

Test script:
---------------
<?php

$optstring = "ab:";

echo "optstring='$optstring'\n";
$args = getopt($optstring);
var_dump($args);
?>

Run it:

php test.php -a -q -b ff -bxx ffff

Note that '-q' is not reported as a bad argument. There is no indication that '-bxx' is the last argument.


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Dec 12 15:01:27 2018 UTC