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
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rik dot davis at logiqnet dot net
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 02 18:01:29 2024 UTC