From d7f715cc36626dce2b3ebdce49744b6b97edfd4d Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Thu, 27 Apr 2023 13:35:34 +0200 Subject: [PATCH 1/3] Fix: install/inc: remove optional argument check htdocs/install/step2.php has two optional argv parameters: action and selectlang. It doesn't have any version indicator, and thus doesn't have any constraint on the number of parameters, so remove it from inc.php. The constraint can still be re-included in the other files including inc.php, as well as the other script-specific options. --- htdocs/install/inc.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/htdocs/install/inc.php b/htdocs/install/inc.php index c0bded73cd2..dcd2462b14f 100644 --- a/htdocs/install/inc.php +++ b/htdocs/install/inc.php @@ -143,13 +143,6 @@ if (php_sapi_name() === "cli") { exit(1); } - // Currently, scripts using inc.php will require addtional arguments, - // see help above for more details. - if ($rest_index > $argc - 2) { - usage($argv[0], "Missing mandatory arguments, usage:"); - exit(1); - } - // Tricky argument list hack, should be removed someday. // Reset argv to remove the argument that were parsed. This is needed // currently because some install code, like in upgrade.php, are using From f19a36856ba8ef791a078b5585e740023e6d67a4 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Thu, 27 Apr 2023 13:35:34 +0200 Subject: [PATCH 2/3] install/inc: add some usage documentation Change the way the existing script syntax is documented and add the syntax and options for the step2.php script. --- htdocs/install/inc.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/htdocs/install/inc.php b/htdocs/install/inc.php index dcd2462b14f..ed1f3f93ad2 100644 --- a/htdocs/install/inc.php +++ b/htdocs/install/inc.php @@ -93,9 +93,20 @@ $long_options = array( function usage($program, $header) { echo $header."\n"; - echo " php ".$program." [options] previous_version new_version [script options]\n"; + echo " php ".$program." [options] [script options]\n"; echo "\n"; - echo "Script options when using upgrade.php:\n"; + echo "Script syntax when using step2.php:\n"; + echo " php ".$program." [options] [action] [selectlang]\n"; + echo "\n"; + echo " action:\n"; + echo " Specify the action to execute for the file among the following ones.\n"; + echo " - set: Create tables, keys, functions and data for the instance.\n"; + echo "\n"; + echo " selectlang:\n"; + echo " Setup the default lang to use, default to 'auto'.\n"; + echo "\n"; + echo "Script syntax when using upgrade.php:\n"; + echo " php ".$program." [options] previous_version new_version [script options]\n"; echo "\n"; echo " dirmodule:\n"; echo " Specify dirmodule to provide a path for an external module\n"; @@ -105,10 +116,11 @@ function usage($program, $header) echo " Allow to run migration even if database version does\n"; echo " not match start version of migration.\n"; echo "\n"; - echo "Script options when using upgrade2.php:\n"; + echo "Script syntax when using upgrade2.php:\n"; + echo " php ".$program." [options] previous_version new_version [module list]\n"; echo "\n"; - echo " MODULE_NAME1_TO_ENABLE,MODULE_NAME2_TO_ENABLE:\n"; - echo " Specify a list of module-name to enable, joined by comma.\n"; + echo " MAIN_MODULE_NAME1,MAIN_MODULE_NAME2:\n"; + echo " Specify a list of module-name to enable, in upper case, with MAIN_MODULE_ prefix, joined by comma.\n"; echo "\n"; echo "Options:\n"; echo " -c, --config :\n"; From 9b2ffafd9314be9635c192e6d09960e373fb9390 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Thu, 27 Apr 2023 14:09:55 +0200 Subject: [PATCH 3/3] 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); }