summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2021-06-15 16:57:36 +0200
committerUrban Müller2021-06-15 16:57:47 +0200
commit5aef64122fbb5a5855af095206bd642ca08cca61 (patch)
tree7e58360b7273d02c3d8e6ef2be78da60926d4464
parentbb27969a215de7f0091f8bd1f63a74107dbb3a1e (diff)
downloaditools-5aef64122fbb5a5855af095206bd642ca08cca61.tar.gz
itools-5aef64122fbb5a5855af095206bd642ca08cca61.tar.bz2
itools-5aef64122fbb5a5855af095206bd642ca08cca61.zip
allow leading - in ::getopt option arguments, add tests
-rw-r--r--it.class7
-rwxr-xr-xtest/it.t26
2 files changed, 27 insertions, 6 deletions
diff --git a/it.class b/it.class
index 2f8c79c..02f17fb 100644
--- a/it.class
+++ b/it.class
@@ -901,12 +901,7 @@ static function getopt($usage, $p = array())
foreach (array_slice($_SERVER['argv'], 1) as $arg)
{
if ($eat)
- {
- if (it::match('^--?\w', $arg)) # Already next option => Missing argument?
- $err = true;
- else
- $result[array_shift($eat)] = $arg;
- }
+ $result[array_shift($eat)] = $arg;
else if ($arg == "--")
$noopts = true;
else if (!$noopts && ($matches = (array)it::match('^--(\w[\w-]*)(=.*)?', $arg)))
diff --git a/test/it.t b/test/it.t
index a3abf32..a6dc067 100755
--- a/test/it.t
+++ b/test/it.t
@@ -509,3 +509,29 @@ function requesturi($teststring, $expect)
it::params2utf8();
is(urldecode($_SERVER['REQUEST_URI']), $expect, "parms2utf8('$expect')");
}
+
+_getopt(["val"], ['foo' => "val"]);
+_getopt(["--", "-val"], ['foo' => "-val"]);
+_getopt(["-v", "val"], ['verbose' => true, 'foo' => "val"]);
+_getopt(["--verbose", "val"], ['verbose' => true, 'foo' => "val"]);
+_getopt(["-vw", "val"], ['verbose' => true, 'werbose' => true, 'foo' => "val"]);
+_getopt(["-a", "val1", "val2"], ['arg' => "val1", 'foo' => "val2"]);
+_getopt(["--arg", "val1", "val2"], ['arg' => "val1", 'foo' => "val2"]);
+_getopt(["--arg=val1", "val2"], ['arg' => "val1", 'foo' => "val2"]);
+_getopt(["-va", "val1", "val2"], ['verbose' => true, 'arg' => "val1", 'foo' => "val2"]);
+_getopt(["-va", "-val1", "val2"], ['verbose' => true, 'arg' => "-val1", 'foo' => "val2"]);
+_getopt(["val1", "val2"], ['args' => ["val2"], 'foo' => 'val1']);
+
+function _getopt($in, $expect)
+{
+ $_SERVER['argv'] = array_merge(["cmd.php"], $in);
+ $opts = it::getopt("
+ Usage: test [OPTIONS] FOO [BAR]
+ -v, --verbose Be verbose
+ -w, --werbose Be werbose
+ -a, --arg=ARG Have argument
+ -b, --brg=ARG Have another arg
+ ");
+
+ is($opts, ['args' => $expect['args'] ?? []] + $expect);
+}