Fix install/inc: detect unknown options
Previous commit 3c883c4b31 added support
for parsing option -- in particular -c/--config -- and added some way of
detecting invalid arguments. But the detection was incorrect.
getopt will stop looking for arguments when it detects a non-argument
(dash-prefixed) which is not an option parameter, but checking that
options were all consumed should be done right after by comparing the
options up to this last non-argument and those that were detected.
This only displays the first unrecognized option, mimicking the
behaviour of most software.
This commit is contained in:
parent
f19a36856b
commit
9b2ffafd93
@ -146,12 +146,26 @@ if (php_sapi_name() === "cli") {
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse the arguments to find the options.
|
||||||
|
$args_options = array_filter(array_slice($argv, 0, $rest_index), function ($arg) {
|
||||||
|
return strlen($arg) >= 2 && $arg[0] == '-';
|
||||||
|
});
|
||||||
|
$parsed_options = array_map(function ($arg) {
|
||||||
|
if (strlen($arg) > 1)
|
||||||
|
return "--" . $arg;
|
||||||
|
return "-" . $arg;
|
||||||
|
}, array_keys($opts));
|
||||||
|
|
||||||
|
// Find options (dash-prefixed) that were not parsed.
|
||||||
|
$unknown_options = array_diff($args_options, $parsed_options);
|
||||||
|
|
||||||
// In the following test, only dash-prefixed arguments will trigger an
|
// In the following test, only dash-prefixed arguments will trigger an
|
||||||
// error, given that scripts options can allow a variable number of
|
// error, given that scripts options can allow a variable number of
|
||||||
// additional non-prefixed argument and we mostly want to check for
|
// additional non-prefixed argument and we mostly want to check for
|
||||||
// typo right now.
|
// typo right now.
|
||||||
if ($rest_index < $argc && $argv[$rest_index][0] == "-") {
|
if (count($unknown_options) > 0) {
|
||||||
usage($argv[0], "Unknown option ".$argv[$rest_index]. ", usage:");
|
echo "Unknown option: ".array_values($unknown_options)[0]."\n";
|
||||||
|
usage($argv[0], "Usage:");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user