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:4
Avg. Score:4.2 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
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
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: 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
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

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 25 12:01:31 2024 UTC