From 948b44ee1e90773ba11d00cb2d348224ee11bc97 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Sat, 10 Mar 2018 16:23:00 +0100 Subject: Change code to be more robust The code doesn't rely on positionnal arguments anymore. It uses options. I've added some check to validate that the action performed are configured properly. --- cli/manipulate.translation.php | 102 ++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/cli/manipulate.translation.php b/cli/manipulate.translation.php index 0e06993ef..19b229878 100644 --- a/cli/manipulate.translation.php +++ b/cli/manipulate.translation.php @@ -1,13 +1,13 @@ load(); -switch ($argv[1]) { - case 'add_language' : - $i18nData->addLanguage($argv[2]); - break; - case 'add_key' : - if (3 === $argc) { - help(); +switch ($options['a']) { + case 'add' : + if (array_key_exists('k', $options) && array_key_exists('v', $options) && array_key_exists('l', $options)) { + $i18nData->addValue($options['k'], $options['v'], $options['l']); + } elseif (array_key_exists('k', $options) && array_key_exists('v', $options)) { + $i18nData->addKey($options['k'], $options['v']); + } elseif (array_key_exists('l', $options)) { + $i18nData->addLanguage($options['l']); + } else { + error('You need to specify a valid set of options.'); } - $i18nData->addKey($argv[2], $argv[3]); break; - case 'add_value': - if (4 === $argc) { - help(); + case 'delete' : + if (array_key_exists('k', $options)) { + $i18nData->removeKey($options['k']); + } else { + error('You need to specify the key to delete.'); } - $i18nData->addValue($argv[2], $argv[3], $argv[4]); - break; - case 'duplicate_key' : - $i18nData->duplicateKey($argv[2]); break; - case 'delete_key' : - $i18nData->removeKey($argv[2]); + case 'duplicate' : + if (array_key_exists('k', $options)) { + $i18nData->duplicateKey($options['k']); + } else { + error('You need to specify the key to duplicate'); + } break; case 'format' : $i18nFile->dump($i18nData); @@ -48,47 +52,61 @@ if ($i18nData->hasChanged()) { $i18nFile->dump($i18nData); } +/** + * Output error message. + */ +function error($message) { + $error = <<