Req #68806 [NEW]: getopt is not able to detect unknown arguments

From: Date: Mon, 12 Jan 2015 09:05:25 +0000
Subject: Req #68806 [NEW]: getopt is not able to detect unknown arguments
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189894@lists.php.net to get a copy of this message
From:             daniel dot marschall at rinntech dot com
Operating system: Debian Squeeze
PHP version:      5.4.36
Package:          Unknown/Other Function
Bug Type:         Feature/Change Request
Bug description:getopt is not able to detect unknown arguments

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"



-- 
Edit bug report at https://bugs.php.net/bug.php?id=68806&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=68806&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=68806&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=68806&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=68806&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=68806&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=68806&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=68806&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=68806&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=68806&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=68806&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=68806&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=68806&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=68806&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68806&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=68806&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=68806&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=68806&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68806&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=68806&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=68806&r=mysqlcfg



Thread (1 message)

  • daniel dot marschall at rinntech dot com
« previous php.bugs (#189894) next »