|  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
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:
Solve the problem:
21 + 32 = ?
Subscribe to this entry?

 [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-2024 The PHP Group
All rights reserved.
Last updated: Sun Mar 03 13:01:28 2024 UTC