php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62940 getopt does not support long options without short options
Submitted: 2012-08-26 18:37 UTC Modified: 2012-08-31 15:44 UTC
Votes:2
Avg. Score:2.0 ± 1.0
Reproduced:0 of 2 (0.0%)
From: b8kich at gmail dot com Assigned:
Status: Not a bug Package: PHP options/info functions
PHP Version: 5.4.6 OS: Linux (Centos 6.3)
Private report: No CVE-ID: None
 [2012-08-26 18:37 UTC] b8kich at gmail dot com
Description:
------------
When only long options are required in getopt (without any short options), they are not captured. 

Found this in 5.3.15 (CentOS 5.8) and confirmed in 5.4.6 (CentOS 6.3)



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

$longopts  = array(
    "required:",     // Required value
    "optional::",    // Optional value
    "option",        // No value
    "opt",           // No value
);
$options = getopt("", $longopts);
var_dump($options);
?>


Expected result:
----------------
The long options should be captured even if there are not short ones:

array(3) {
  ["required"]=>
  string(5) "value"
  ["optional"]=>
  string(14) "optional value"
  ["option"]=>
  bool(false)
}

Actual result:
--------------
$ php script.php -f "value for f" --required value --optional="optional value" --option
array(0) {
}

Throwing in some short options does not help either:
$ php script.php -f "value for f" -v -a --required value --optional="optional value" --option
array(0) {
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-30 20:26 UTC] lonnyk at gmail dot com
I am not having any problems with this:

$ php -v
PHP 5.4.6RC1 (cli) (built: Aug 30 2012 20:24:11) (DEBUG)

$ php ../tests/issue-62940.php --required value --optional="optional value" --
option
array(3) {
  ["required"]=>
  string(5) "value"
  ["optional"]=>
  string(14) "optional value"
  ["option"]=>
  bool(false)
}


Can you please provide the _exact_ command you are using for calling the script?
 [2012-08-31 15:12 UTC] b8kich at gmail dot com
I see the issue is I had an extra option before the long options in the command I've used to test. Getopt stops parsing on the unrecognized option. This is actually documented, let's close this out. 

$ php script.php -f "value for f" --required value --optional="optional value" --option
 [2012-08-31 15:13 UTC] b8kich at gmail dot com
Getopt stops parsing on the unrecognized (short) option as documented.
 [2012-08-31 15:13 UTC] b8kich at gmail dot com
-Status: Open +Status: Closed
 [2012-08-31 15:44 UTC] rasmus@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC