php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73481 getopt function argument handling
Submitted: 2016-11-08 18:05 UTC Modified: 2018-02-28 22:13 UTC
From: rik dot davis at logiqnet dot net Assigned:
Status: Open Package: PHP options/info functions
PHP Version: 5.6.27 OS: Centos 7.2.1511
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
40 - 13 = ?
Subscribe to this entry?

 
 [2016-11-08 18:05 UTC] rik dot davis at logiqnet dot net
Description:
------------
---
From manual page: http://www.php.net/function.getopt
---


Test script:
---------------
Save as test.php
<?php
$opts = getopt("f::");

print_r($opts);
?>

Working Example:
php test.php -ffilename.ext

Breaking Example 1:
php test.php -f filename.ext

Breaking Example 2:
php test.php myarg -f filename.ext

Expected result:
----------------
All examples should return the following;

Array
(
  [f] => filename.ext
)

Actual result:
--------------
Working Example:
Array
(
  [f] => filename.ext
)

Breaking Example 1:
> Does not allow for options to have space separated singular values.
Array
(
  [f] =>
)

Breaking Example 2:
> Does not allow for options to exist after non-options arguments.
Array
(
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-20 21:57 UTC] php at duncanc dot co dot uk
For breaking example 1 there's a note in the source that states:
"Optional value is not supported with -<arg> <val> style"
https://github.com/php/php-src/blob/45f7b2bcc8f95e793c1a9bb60da0848860c060fb/main/getopt.c#L167

For breaking example 2, I think any positional arguments must follow any named arguments, so you'd need to do:
php test.php -ffilename.ext myarg
 [2018-02-28 22:13 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: PHP options/info functions
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Aug 19 02:01:28 2019 UTC