From 9b2ffafd9314be9635c192e6d09960e373fb9390 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Thu, 27 Apr 2023 14:09:55 +0200 Subject: [PATCH] Fix install/inc: detect unknown options Previous commit 3c883c4b319c1742fa8d4403200081b0119a4f3a 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. --- htdocs/install/inc.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/htdocs/install/inc.php b/htdocs/install/inc.php index ed1f3f93ad2..1133f10182e 100644 --- a/htdocs/install/inc.php +++ b/htdocs/install/inc.php @@ -146,12 +146,26 @@ if (php_sapi_name() === "cli") { 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 // error, given that scripts options can allow a variable number of // additional non-prefixed argument and we mostly want to check for // typo right now. - if ($rest_index < $argc && $argv[$rest_index][0] == "-") { - usage($argv[0], "Unknown option ".$argv[$rest_index]. ", usage:"); + if (count($unknown_options) > 0) { + echo "Unknown option: ".array_values($unknown_options)[0]."\n"; + usage($argv[0], "Usage:"); exit(1); }