diff options
author | Nathan Gass | 2020-08-25 15:57:32 +0200 |
---|---|---|
committer | Nathan Gass | 2020-08-25 15:58:47 +0200 |
commit | f42d056812d4c1766d75bd8ab6d871e06a700302 (patch) | |
tree | d56641bda4ea49c7432d27707a2fab50b58c8265 | |
parent | 3f6c52f92f0026f383b53577a9e015b6495e89c3 (diff) | |
download | itools-f42d056812d4c1766d75bd8ab6d871e06a700302.tar.gz itools-f42d056812d4c1766d75bd8ab6d871e06a700302.tar.bz2 itools-f42d056812d4c1766d75bd8ab6d871e06a700302.zip |
test for unexpected optional positinal arguments
-rw-r--r-- | it.class | 8 | ||||
-rwxr-xr-x | test/getopt.t | 10 |
2 files changed, 14 insertions, 4 deletions
@@ -874,9 +874,9 @@ static function getopt($usage, $p = array()) } $witharg['debug'] = true; - $mandatoryargs = array(); - if ($tmp = trim(it::replace(array("\n.*" => "", "^\S+\s+\S+\s*" => "", "\[.*?\]\s*" => ""), trim($usage)))) - $mandatoryargs = preg_split('/\s+/', $tmp); + $argsdesc = trim(it::replace(array("\n.*" => "", "^\S+\s+\S+\s*" => ""), trim($usage))); + $mandatoryargs = it::match('\S+', it::replace(['\[.*?\]' => ''], $argsdesc), ['all' => true]); + $optionalargs = it::match('\[((?!OPTIONS\]).*?)\]', $argsdesc, ['all' => true]); if ($mandatoryargs && !it::match("Usage:", $usage)) it::error("Usage string must contain 'Usage:'"); @@ -925,6 +925,8 @@ static function getopt($usage, $p = array()) $noopts = true; } + if (!$optionalargs && $result['args']) + it::error("Optional arguments passed to script without optional args in usage"); # FIXME 2020-10 NG merge with normal usage errors below if ($err || $eat || $result['h'] || $result['help'] || $mandatoryargs) { fputs(($result['h'] || $result['help'] ? STDOUT : STDERR), trim($usage) . "\n"); diff --git a/test/getopt.t b/test/getopt.t index e74e925..aa2de52 100755 --- a/test/getopt.t +++ b/test/getopt.t @@ -3,7 +3,7 @@ # Tests for getopt in it.class -$GLOBALS['usage'] = "Usage: doesnotexist.php [OPTIONS] POSITIONAL +$GLOBALS['usage'] = "Usage: doesnotexist.php [OPTIONS] POSITIONAL [VARARGS] Some help to a not existing program -h,--help the help argument -a,--argument=ARG the arg argument @@ -37,3 +37,11 @@ getopt_ok(['posarg', '-u'], false, "Unknown short named argument fails"); getopt_ok([], false, "Missing positional argument fails"); getopt_ok(['posarg', '--argument'], false, "Missing long named argument fails"); getopt_ok(['posarg', '-a'], false, "Missing short named argument fails"); + +// FIXME 2020-10 NG enable after fixme in it.class +// $GLOBALS['usage'] = it::replace('\s*\[VARARGS\]' => '', $GLOBALS['usage']); +// getopt_ok(['posargs', 'vararg'], false, "Extra positional argument fails"); +// getopt_ok(['posargs', '--zero', 'vararg'], false, "Extra positional argument fails after long argument"); +// getopt_ok(['posargs', '-0', 'vararg'], false, "Extra positional argument fails after short argument"); +// getopt_ok(['posargs', '--argument', 'value', 'vararg'], false, "Extra positional argument fails after long argument with value"); +// getopt_ok(['posargs', '-a', 'value', 'vararg'], false, "Extra positional argument fails after short argument with value"); |