aboutsummaryrefslogtreecommitdiff
path: root/cli/manipulate.translation.php
diff options
context:
space:
mode:
authorGravatar Kasimir Cash <kasimir.cash@outlook.com> 2024-02-28 12:23:28 +0000
committerGravatar GitHub <noreply@github.com> 2024-02-28 13:23:28 +0100
commit4b29e666b06762b4b36438c9370c38bc43121f78 (patch)
tree2b92dcbc5879aa7de8eeec81ccde208f572b3cf0 /cli/manipulate.translation.php
parent5de794ee0fbbce2fdf0af3787b9b89299be8698e (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/manipulate.translation.php')
-rwxr-xr-xcli/manipulate.translation.php103
1 files changed, 50 insertions, 53 deletions
diff --git a/cli/manipulate.translation.php b/cli/manipulate.translation.php
index 358a3ec33..740f3c4b7 100755
--- a/cli/manipulate.translation.php
+++ b/cli/manipulate.translation.php
@@ -6,70 +6,65 @@ require_once __DIR__ . '/i18n/I18nData.php';
require_once __DIR__ . '/i18n/I18nFile.php';
require_once __DIR__ . '/../constants.php';
-$parameters = [
- 'long' => [
- 'action' => ':',
- 'help' => '',
- 'key' => ':',
- 'language' => ':',
- 'origin-language' => ':',
- 'revert' => '',
- 'value' => ':',
- ],
- 'short' => [
- 'action' => 'a',
- 'help' => 'h',
- 'key' => 'k',
- 'language' => 'l',
- 'origin-language' => 'o',
- 'revert' => 'r',
- 'value' => 'v',
- ],
- 'deprecated' => [],
-];
-
-$options = parseCliParams($parameters);
-
-if (!empty($options['invalid']) || array_key_exists('help', $options['valid'])) {
- manipulateHelp();
- exit();
+$cliOptions = new class extends CliOptionsParser {
+ public string $action;
+ public string $key;
+ public string $value;
+ public string $language;
+ public string $originLanguage;
+ public string $revert;
+ public string $help;
+
+ public function __construct() {
+ $this->addRequiredOption('action', (new CliOption('action', 'a')));
+ $this->addOption('key', (new CliOption('key', 'k')));
+ $this->addOption('value', (new CliOption('value', 'v')));
+ $this->addOption('language', (new CliOption('language', 'l')));
+ $this->addOption('originLanguage', (new CliOption('origin-language', 'o')));
+ $this->addOption('revert', (new CliOption('revert', 'r'))->withValueNone());
+ $this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
+ parent::__construct();
+ }
+};
+
+if (!empty($cliOptions->errors)) {
+ fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
}
-
-if (!array_key_exists('action', $options['valid'])) {
- error('You need to specify the action to perform.');
+if (isset($cliOptions->help)) {
+ manipulateHelp();
}
$data = new I18nFile();
$i18nData = new I18nData($data->load());
-switch ($options['valid']['action']) {
+switch ($cliOptions->action) {
case 'add' :
- if (array_key_exists('key', $options['valid']) && array_key_exists('value', $options['valid']) && array_key_exists('language', $options['valid'])) {
- $i18nData->addValue($options['valid']['key'], $options['valid']['value'], $options['valid']['language']);
- } elseif (array_key_exists('key', $options['valid']) && array_key_exists('value', $options['valid'])) {
- $i18nData->addKey($options['valid']['key'], $options['valid']['value']);
- } elseif (array_key_exists('language', $options['valid'])) {
+ if (isset($cliOptions->key) && isset($cliOptions->value) && isset($cliOptions->language)) {
+ $i18nData->addValue($cliOptions->key, $cliOptions->value, $cliOptions->language);
+ } elseif (isset($cliOptions->key) && isset($cliOptions->value)) {
+ $i18nData->addKey($cliOptions->key, $cliOptions->value);
+ } elseif (isset($cliOptions->language)) {
$reference = null;
- if (array_key_exists('origin-language', $options['valid'])) {
- $reference = $options['valid']['origin-language'];
+ if (isset($cliOptions->originLanguage)) {
+ $reference = $cliOptions->originLanguage;
}
- $i18nData->addLanguage($options['valid']['language'], $reference);
+ $i18nData->addLanguage($cliOptions->language, $reference);
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
case 'delete' :
- if (array_key_exists('key', $options['valid'])) {
- $i18nData->removeKey($options['valid']['key']);
+ if (isset($cliOptions->key)) {
+ $i18nData->removeKey($cliOptions->key);
} else {
error('You need to specify the key to delete.');
exit;
}
break;
case 'exist':
- if (array_key_exists('key', $options['valid'])) {
- $key = $options['valid']['key'];
+ if (isset($cliOptions->key)) {
+ $key = $cliOptions->key;
if ($i18nData->isKnown($key)) {
echo "The '{$key}' key is known.\n\n";
} else {
@@ -83,16 +78,16 @@ switch ($options['valid']['action']) {
case 'format' :
break;
case 'ignore' :
- if (array_key_exists('language', $options['valid']) && array_key_exists('key', $options['valid'])) {
- $i18nData->ignore($options['valid']['key'], $options['valid']['language'], array_key_exists('revert', $options['valid']));
+ if (isset($cliOptions->language) && isset($cliOptions->key)) {
+ $i18nData->ignore($cliOptions->key, $cliOptions->language, isset($cliOptions->revert));
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
case 'ignore_unmodified' :
- if (array_key_exists('language', $options['valid'])) {
- $i18nData->ignore_unmodified($options['valid']['language'], array_key_exists('revert', $options['valid']));
+ if (isset($cliOptions->language)) {
+ $i18nData->ignore_unmodified($cliOptions->language, isset($cliOptions->revert));
} else {
error('You need to specify a valid set of options.');
exit;
@@ -122,6 +117,7 @@ ERROR;
*/
function manipulateHelp(): void {
$file = str_replace(__DIR__ . '/', '', __FILE__);
+
echo <<<HELP
NAME
$file
@@ -144,17 +140,17 @@ DESCRIPTION
select the origin language (only for add language action)
EXAMPLES
-Example 1: add a language. It adds a new language by duplicating the referential.
+Example 1: add a language. Adds a new language by duplicating the reference language.
php $file -a add -l my_lang
php $file -a add -l my_lang -o ref_lang
-Example 2: add a new key. It adds the key for all supported languages.
+Example 2: add a new key. Adds a key to all supported languages.
php $file -a add -k my_key -v my_value
-Example 3: add a new value. It adds a new value for the selected key in the selected language.
+Example 3: add a new value. Sets a new value for the selected key in the selected language.
php $file -a add -k my_key -v my_value -l my_lang
-Example 4: delete a key. It deletes the selected key from all supported languages.
+Example 4: delete a key. Deletes the selected key from all supported languages.
php $file -a delete -k my_key
Example 5: format i18n files.
@@ -170,11 +166,12 @@ Example 8: ignore all unmodified keys. Adds IGNORE comments to all unmodified ke
php $file -a ignore_unmodified -l my_lang
Example 9: revert ignore on all unmodified keys. Removes IGNORE comments from all unmodified keys in the selected language.
- Warning: will also revert individually added unmodified keys.
+ Warning: will also revert individually added IGNOREs on unmodified keys.
php $file -a ignore_unmodified -r -l my_lang
Example 10: check if a key exist.
- php $file -a exist -k my_key\n\n
+ php $file -a exist -k my_key
HELP;
+ exit();
}