mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-04-30 11:14:43 -04:00
2297 cli parameters consistency (#6028)
* Updates do-install params * Adds parseCliParams to _cli.php * Updates do-install to use parseCliParams * Updates reconfigure to use parseCliParams * Fixes bug mail_login => email * Update create-user to use parseCliParams * Minor refactor * Updates update-user to use parseCliParams * Fix no_default_feeds => no-default-feeds * Refactors arrays * Updates CLI Readme * Adds docblocks to _cli functions * Sets vars in _cli functions * Fixes indentation * Meeting coding standards around colons * Meeting PHPStan standards * Removes stray whitespace * Meeting PHPStan Next Level standards * More specific typing * Maintaining types * Typing around getopt() * Fixes typo * Fixes typo perameters -> parameters * Removes unused variable * Rewrites deprecation warning message
This commit is contained in:
106
cli/_cli.php
106
cli/_cli.php
@@ -75,6 +75,47 @@ function performRequirementCheck(string $databaseType): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses parameters used with FreshRSS' CLI commands.
|
||||
* @param array{'valid':array<string,string>,'deprecated':array<string,string>} $parameters An array of 'valid': An
|
||||
* array of parameters as keys and their respective getopt() notations as values. 'deprecated' An array with
|
||||
* replacement parameters as keys and their respective deprecated parameters as values.
|
||||
* @return array{'valid':array<string,string|bool>,'invalid':array<string>} An array of 'valid': an array of all
|
||||
* known parameters used and their respective options and 'invalid': an array of all unknown parameters used.
|
||||
*/
|
||||
function parseCliParams(array $parameters): array {
|
||||
global $argv;
|
||||
$cliParams = [];
|
||||
|
||||
foreach ($parameters['valid'] as $param => $getopt_val) {
|
||||
$cliParams[] = $param . $getopt_val;
|
||||
}
|
||||
foreach ($parameters['deprecated'] as $param => $deprecatedParam) {
|
||||
$cliParams[] = $deprecatedParam . $parameters['valid'][$param];
|
||||
}
|
||||
|
||||
$opts = getopt('', $cliParams);
|
||||
|
||||
/** @var array<string,string|bool> $valid */
|
||||
$valid = is_array($opts) ? $opts : [];
|
||||
|
||||
array_walk($valid, static fn(&$option) => $option = $option === false ? true : $option);
|
||||
|
||||
if (checkforDeprecatedParameterUse(array_keys($valid), $parameters['deprecated'])) {
|
||||
$valid = updateDeprecatedParameters($valid, $parameters['deprecated']);
|
||||
}
|
||||
|
||||
$invalid = findInvalidOptions(
|
||||
$argv,
|
||||
array_merge(array_keys($parameters['valid']), array_values($parameters['deprecated']))
|
||||
);
|
||||
|
||||
return [
|
||||
'valid' => $valid,
|
||||
'invalid' => $invalid
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $options
|
||||
* @return array<string>
|
||||
@@ -104,3 +145,68 @@ function validateOptions(array $input, array $params): bool {
|
||||
fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions)));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for use of unknown parameters with FreshRSS' CLI commands.
|
||||
* @param array<string> $input An array of parameters to check for validity.
|
||||
* @param array<string> $params An array of valid parameters to check against.
|
||||
* @return array<string> Returns an array of all unknown parameters found.
|
||||
*/
|
||||
function findInvalidOptions(array $input, array $params): array {
|
||||
$sanitizeInput = getLongOptions($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 use of deprecated parameters with FreshRSS' CLI commands.
|
||||
* @param array<string> $options User inputs to check for deprecated parameter use.
|
||||
* @param array<string,string> $params An array with replacement parameters as keys and their respective deprecated
|
||||
* parameters as values.
|
||||
* @return bool Returns TRUE and generates a deprecation warning if deprecated parameters
|
||||
* have been used, FALSE otherwise.
|
||||
*/
|
||||
function checkforDeprecatedParameterUse(array $options, array $params): bool {
|
||||
$deprecatedOptions = array_intersect($options, $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 all used deprecated parameters to their replacements if they have one.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @param array<string,T> $options User inputs.
|
||||
* @param array<string,string> $params An array with replacement parameters as keys and their respective deprecated
|
||||
* parameters as values.
|
||||
* @return array<string,T> Returns $options with deprications replaced.
|
||||
*/
|
||||
function updateDeprecatedParameters(array $options, array $params): array {
|
||||
$updatedOptions = [];
|
||||
|
||||
foreach ($options as $param => $option) {
|
||||
$replacement = array_search($param, $params, true);
|
||||
if (is_string($replacement)) {
|
||||
$updatedOptions[$replacement] = $option;
|
||||
} else {
|
||||
$updatedOptions[$param] = $option;
|
||||
}
|
||||
}
|
||||
|
||||
return $updatedOptions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user