Files
FreshRSS/cli/manipulate.translation.php
Kasimir Cash 4b29e666b0 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>
2024-02-28 13:23:28 +01:00

178 lines
5.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/_cli.php';
require_once __DIR__ . '/i18n/I18nData.php';
require_once __DIR__ . '/i18n/I18nFile.php';
require_once __DIR__ . '/../constants.php';
$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 (isset($cliOptions->help)) {
manipulateHelp();
}
$data = new I18nFile();
$i18nData = new I18nData($data->load());
switch ($cliOptions->action) {
case 'add' :
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 (isset($cliOptions->originLanguage)) {
$reference = $cliOptions->originLanguage;
}
$i18nData->addLanguage($cliOptions->language, $reference);
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
case 'delete' :
if (isset($cliOptions->key)) {
$i18nData->removeKey($cliOptions->key);
} else {
error('You need to specify the key to delete.');
exit;
}
break;
case 'exist':
if (isset($cliOptions->key)) {
$key = $cliOptions->key;
if ($i18nData->isKnown($key)) {
echo "The '{$key}' key is known.\n\n";
} else {
echo "The '{$key}' key is unknown.\n\n";
}
} else {
error('You need to specify the key to check.');
exit;
}
break;
case 'format' :
break;
case 'ignore' :
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 (isset($cliOptions->language)) {
$i18nData->ignore_unmodified($cliOptions->language, isset($cliOptions->revert));
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
default :
manipulateHelp();
exit;
}
$data->dump($i18nData->getData());
/**
* Output error message.
*/
function error(string $message): void {
$error = <<<ERROR
WARNING
%s\n\n
ERROR;
echo sprintf($error, $message);
manipulateHelp();
}
/**
* Output help message.
*/
function manipulateHelp(): void {
$file = str_replace(__DIR__ . '/', '', __FILE__);
echo <<<HELP
NAME
$file
SYNOPSIS
php $file [OPTIONS]
DESCRIPTION
Manipulate translation files.
-a, --action=ACTION
select the action to perform. Available actions are add, delete,
exist, format, ignore, and ignore_unmodified. This option is mandatory.
-k, --key=KEY select the key to work on.
-v, --value=VAL select the value to set.
-l, --language=LANG select the language to work on.
-h, --help display this help and exit.
-r, --revert revert the action (only for ignore action)
-o, origin-language=LANG
select the origin language (only for add language action)
EXAMPLES
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. Adds a key to all supported languages.
php $file -a add -k my_key -v my_value
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. Deletes the selected key from all supported languages.
php $file -a delete -k my_key
Example 5: format i18n files.
php $file -a format
Example 6: ignore a key. Adds IGNORE comment to the key in the selected language, marking it as translated.
php $file -a ignore -k my_key -l my_lang
Example 7: revert ignore a key. Removes IGNORE comment from the key in the selected language.
php $file -a ignore -r -k my_key -l my_lang
Example 8: ignore all unmodified keys. Adds IGNORE comments to all unmodified keys in the selected language, marking them as translated.
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 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
HELP;
exit();
}