php.net |  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: -
Votes:3
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:
MUST BE VALID
Solve the problem:
29 - 26 = ?
Subscribe to this entry?

 
 [2015-01-12 09:05 UTC] daniel dot marschall at rinntech dot com
Description:
------------
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. (http://php.net/manual/de/function.getopt.php#102211)

There are only dirty workarounds possible, which are not able to deal with all kinds of argument notation resp. value-handling: http://stackoverflow.com/a/25388917/3544341 .


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". (http://unix.stackexchange.com/questions/55753/how-to-deal-with-end-of-options-in-getopts)

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

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

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

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



Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 15:01:28 2024 UTC