PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Console » Console_Getopt » Bug #4475

An ambiguous error occurred when specifying similar longoption name.

Details

Submitted2005-05-30 09:57 UTC
Fromryo at fastriver dot net
Assignedcellog
StatusClosed
PackageConsole_Getopt
PHP Version4.3.11
OSSlackware Linux 9.0
Roadmaps(Not assigned)

Comments

[2005-05-30 09:57 UTC] ryo at fastriver dot net

Description:
------------
I'm a Japanese. I'm sorry, my English is poor,
so it may be hard to read this.

For example, I give the following similar longoptions parameter to getopt().

$longoptions = array("last_name=", "last_name_two=");

And I execute my php script as follows.

php myscript.php --last_name="abc"

Then, getopt() return a PEAR error object. The error
message of the error object is as follows.

Console_Getopt: option --last_name is ambiguous

Reproduce code:
---------------
--- Getopt.php.orig 2003-12-12 04:21:14.000000000 +0900
+++ Getopt.php 2005-05-30 18:43:51.000000000 +0900
@@ -201,7 +201,7 @@
options. */
if ($opt_rest != '' && $opt{0} != '=' &&
$i + 1 < count($long_options) &&
- $opt == substr($long_options[$i+1], 0, $opt_len)) {
+ $opt == substr($long_options[$i+1], 0, strlen($long_options[$i+1]))) {
return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
}

Expected result:
----------------
getopt() should not return the error object.

Actual result:
--------------
getopt() returns the error object.

[2006-07-26 07:07 UTC] mleegwater at gmail dot com

Hi there!

A patch that could solve this problem:

--- /usr/share/php/Console/Getopt.php 2006-04-16 13:19:46.000000000 +0200
+++ Getopt.php 2006-07-26 08:50:57.000000000 +0200
@@ -189,19 +189,21 @@

for ($i = 0; $i < count($long_options); $i++) {
$long_opt = $long_options[$i];
- $opt_start = substr($long_opt, 0, $opt_len);
+ $long_opt_name = preg_replace("/=/", "", $long_opt);

- /* Option doesn't match. Go on to the next one. */
- if ($opt_start != $opt)
- continue;
+ if ($long_opt_name != $opt)
+ continue;

$opt_rest = substr($long_opt, $opt_len);

/* Check that the options uniquely matches one of the allowed
options. */
+ $next_option_rest = substr($long_options[$i+1],$opt_len);
if ($opt_rest != '' && $opt{0} != '=' &&
$i + 1 < count($long_options) &&
- $opt == substr($long_options[$i+1], 0, $opt_len)) {
+ $opt == substr($long_options[$i+1], 0, $opt_len) &&
+ $next_option_rest != '' &&
+ $next_option_rest{0} != '=') {
return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
}

Regards,

Michiel