|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68806 getopt is not able to detect unknown arguments
Submitted: 2015-01-12 09:05 UTC Modified: -
Avg. Score:4.0 ± 0.8
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: daniel dot marschall at rinntech dot com Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: 5.4.36 OS: Debian Squeeze
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: daniel dot marschall at rinntech dot com
New email:
PHP Version: OS:


 [2015-01-12 09:05 UTC] daniel dot marschall at rinntech dot com
I am developing a CLI application which must output an usage-description as soon as the user inputs an invalid command (which is e.g. -? ), this is a must criteria.

First problem:

getopt() only returns the options specified if they were listed in the options.

So you can't make a switch() use "default:" to complain of an unknown option. (

There are only dirty workarounds possible, which are not able to deal with all kinds of argument notation resp. value-handling: .

Second problem:

Additionally, there doesn't seem a comfortable way to handle the "end of options" (--) functionality to handle rest arguments, e.g. "./script --verbose -- file1.jpg file2.jpg". (

The only workaround I see is to manually parse "--", but there are caveats, e.g. if "--" appears inside an argument value.

Test script:

$x = getopt('v', 'verbose');

// call script with "./myscript.php -?" or "./myscript.php -xyz"


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Oct 03 18:01:28 2023 UTC