diff options
| author | 2024-02-28 12:23:28 +0000 | |
|---|---|---|
| committer | 2024-02-28 13:23:28 +0100 | |
| commit | 4b29e666b06762b4b36438c9370c38bc43121f78 (patch) | |
| tree | 2b92dcbc5879aa7de8eeec81ccde208f572b3cf0 /cli/_cli.php | |
| parent | 5de794ee0fbbce2fdf0af3787b9b89299be8698e (diff) | |
Command Line Parser Concept (#6099)
* Adds logic for validation
* Adds validation to do-install
* Adds help to do-install
* Adds validation & help to reconfigure
* Adds validation to check.translation
* Adds validation to manipulate.translation
* Small fixes to help texts
* Refactors language option validation
* Adds default options to validation
* Fixes validation with regex
* Refactors readAs functions
* Updates to new regex validation format
* Fixes typing around default values
* Adds file extension validation
* Restandardises validation & parsing typing around array of strings
* Adds NotOneOf validation
* Adds ArrayOfString read as
* Refactors existing validation
* Adds validation throughout cli
* Removes unused file
* Adds new CL parser with goal of wrapping CLI behaviour
* Hides parsing and validation
* Rewites CL parser to make better use of classes
* Rolls out new parser across CL
* Fixes error during unknown option check
* Fixes misnamed property calls
* Seperates validations into more appropriate locations
* Adds common boolean forms to validation
* Moves CommandLineParser and Option classes into their own files
* Fixes error when validating Int type
* Rewrites appendTypedValues -> appendTypedValidValues now filters invalid values from output
* Renames -> for clarity
* Adds some docs clarifying option defaults and value taking behaviour
* Refactors getUsageMessage for readability
* Minor formatting changes
* Adds tests for CommandLineParser
* Adds more tests
* Adds minor fixs
* Reconfigure now correctly updates config
* More fixes to reconfigure
* Fixes required files for CommandLineParserTest
* Use .php extension for PHP file
* PHPStan ignore instead of wrong typing
* Refactors to support php 7.4
* Moves away from dynamic properties by adding 'Definintions' to all commands
* Renames target to definition for clarity
* Stops null from being returned as a valid value in a certain edge case
* Adds PHPStan ignore instead of incorrect typing
* Refactors tests to take account of new typing solution
* Marks file as executable
* Draft CLI rework
* Finish rewrite as object-oriented
* Fix PHPStan ignore and make more strongly typed
* Rename class Option to CliOption
* Light renaming + anonymous classes
---------
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'cli/_cli.php')
| -rwxr-xr-x[-rw-r--r--] | cli/_cli.php | 119 |
1 files changed, 2 insertions, 117 deletions
diff --git a/cli/_cli.php b/cli/_cli.php index c51dd69a3..9d9d9c32d 100644..100755 --- a/cli/_cli.php +++ b/cli/_cli.php @@ -6,11 +6,12 @@ if (php_sapi_name() !== 'cli') { } const EXIT_CODE_ALREADY_EXISTS = 3; -const REGEX_INPUT_OPTIONS = '/^-{2}|^-{1}/'; require(__DIR__ . '/../constants.php'); require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader require(LIB_PATH . '/lib_install.php'); +require_once(__DIR__ . '/CliOption.php'); +require_once(__DIR__ . '/CliOptionsParser.php'); Minz_Session::init('FreshRSS', true); FreshRSS_Context::initSystem(); @@ -73,119 +74,3 @@ function performRequirementCheck(string $databaseType): void { fail($message); } } - -/** - * Parses parameters used with FreshRSS' CLI commands. - * @param array{'long':array<string,string>,'short':array<string,string>,'deprecated':array<string,string>} $parameters - * Matrix of 'long': map of long option names as keys and their respective getopt() notations as values, - * 'short': map of short option names as values and their equivalent long options as keys, 'deprecated': map of - * replacement option names as keys and their respective deprecated option names as values. - * @return array{'valid':array<string,string>,'invalid':array<string>} Matrix of 'valid': map of of all known - * option names used and their respective values and 'invalid': list of all unknown options used. - */ -function parseCliParams(array $parameters): array { - global $argv; - $longOptions = []; - $shortOptions = ''; - - foreach ($parameters['long'] as $name => $getopt_note) { - $longOptions[] = $name . $getopt_note; - } - foreach ($parameters['deprecated'] as $name => $deprecatedName) { - $longOptions[] = $deprecatedName . $parameters['long'][$name]; - } - foreach ($parameters['short'] as $name => $shortName) { - $shortOptions .= $shortName . $parameters['long'][$name]; - } - - $options = getopt($shortOptions, $longOptions); - - $valid = is_array($options) ? $options : []; - - array_walk($valid, static fn(&$option) => $option = $option === false ? '' : $option); - - /** @var array<string,string> $valid */ - checkForDeprecatedOptions(array_keys($valid), $parameters['deprecated']); - - $valid = replaceOptions($valid, $parameters['short']); - $valid = replaceOptions($valid, $parameters['deprecated']); - - $invalid = findInvalidOptions( - $argv, - array_merge(array_keys($parameters['long']), array_values($parameters['short']), array_values($parameters['deprecated'])) - ); - - return [ - 'valid' => $valid, - 'invalid' => $invalid - ]; -} - -/** - * @param array<string> $options - * @return array<string> - */ -function getOptions(array $options, string $regex): array { - $longOptions = array_filter($options, static function (string $a) use ($regex) { - return preg_match($regex, $a) === 1; - }); - return array_map(static function (string $a) use ($regex) { - return preg_replace($regex, '', $a) ?? ''; - }, $longOptions); -} - -/** - * Checks for presence of unknown options. - * @param array<string> $input List of command line arguments to check for validity. - * @param array<string> $params List of valid options to check against. - * @return array<string> Returns a list all unknown options found. - */ -function findInvalidOptions(array $input, array $params): array { - $sanitizeInput = getOptions($input, REGEX_INPUT_OPTIONS); - $unknownOptions = array_diff($sanitizeInput, $params); - - if (0 === count($unknownOptions)) { - return []; - } - - fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions))); - return $unknownOptions; -} - -/** - * Checks for presence of deprecated options. - * @param array<string> $optionNames Command line option names to check for deprecation. - * @param array<string,string> $params Map of replacement options as keys and their respective deprecated - * options as values. - * @return bool Returns TRUE and generates a deprecation warning if deprecated options are present, FALSE otherwise. - */ -function checkForDeprecatedOptions(array $optionNames, array $params): bool { - $deprecatedOptions = array_intersect($optionNames, $params); - $replacements = array_map(static fn($option) => array_search($option, $params, true), $deprecatedOptions); - - if (0 === count($deprecatedOptions)) { - return false; - } - - fwrite(STDERR, "FreshRSS deprecation warning: the CLI option(s): " . implode(', ', $deprecatedOptions) . - " are deprecated and will be removed in a future release. Use: " - . implode(', ', $replacements) . " instead\n"); - return true; -} - -/** - * Switches items in a list to their provided replacements. - * @param array<string,string> $options Map with items to check for replacement as keys. - * @param array<string,string> $replacements Map of replacement items as keys and the item they replace as their values. - * @return array<string,string> Returns $options with replacements. - */ -function replaceOptions(array $options, array $replacements): array { - $updatedOptions = []; - - foreach ($options as $name => $value) { - $replacement = array_search($name, $replacements, true); - $updatedOptions[$replacement ? $replacement : $name] = $value; - } - - return $updatedOptions; -} |
