diff options
-rw-r--r-- | it.class | 4 | ||||
-rwxr-xr-x | tests/getopt.t | 24 |
2 files changed, 26 insertions, 2 deletions
@@ -518,7 +518,7 @@ function getopt($helplines) $witharg[$longoptname ? $longoptname : $shortoptname] = $longoptarg || $shortoptarg; } } - $witharg['debug'] = 1; + $witharg['debug'] = true; $mandatoryargs = array(); if ($tmp = trim(it::replace(array("\n.*" => "", "^\S+\s+\S+\s*" => "", "\[.*?\]\s*" => ""), trim($helplines)))) @@ -533,7 +533,7 @@ function getopt($helplines) else $result[array_shift($eat)] = $arg; } - elseif ($matches = (array)it::match('^--(\w[\w-]*)(=\S*)?', $arg)) + elseif ($matches = (array)it::match('^--(\w[\w-]*)(=.*)?', $arg)) { list($optname, $val) = $matches; if (!isset($witharg[$optname]) || isset($val) && !$witharg[$optname]) diff --git a/tests/getopt.t b/tests/getopt.t new file mode 100755 index 0000000..41e985d --- /dev/null +++ b/tests/getopt.t @@ -0,0 +1,24 @@ +#!/www/server/bin/php -qC +<?php + +# Tests for getopt in it.class + +require 'searchlib/search_test.class'; + +$usage = "Usage: doesnotexist.php [OPTIONS] +Some help to a not existing program + -h,--help the help argument + -a,--argument=ARG the arg argument +"; + +function getopt_ok($argv, $exp, $name) +{ + $_SERVER['argv'] = array_merge(array('doesnotexist.php'), $argv); + $got = it::getopt($GLOBALS['usage']); + return is($got['argument'], $exp, $name); +} + +$testarg = "blah gnaber"; +getopt_ok(array('-a', $testarg), $testarg, "Short version"); +getopt_ok(array('--argument', $testarg), $testarg, "Long version with space"); +getopt_ok(array("--argument=$testarg"), $testarg, "Long version with equal"); |