<commit_before><?php

$options = getopt("h");

if (array_key_exists('h', $options)) {
	help();
}

if (1 === $argc || 5 < $argc) {
	help();
}

require_once __DIR__ . '/i18n/I18nFile.php';

$i18nFile = new I18nFile();
$i18nData = $i18nFile->load();

switch ($argv[1]) {
	case 'add_language' :
		$i18nData->addLanguage($argv[2]);
		break;
	case 'add_key' :
		if (3 === $argc) {
			help();
		}
		$i18nData->addKey($argv[2], $argv[3]);
		break;
	case 'add_value':
		if (4 === $argc) {
			help();
		}
		$i18nData->addValue($argv[2], $argv[3], $argv[4]);
		break;
	case 'duplicate_key' :
		$i18nData->duplicateKey($argv[2]);
		break;
	case 'delete_key' :
		$i18nData->removeKey($argv[2]);
		break;
	case 'format' :
		$i18nFile->dump($i18nData);
		break;
	default :
		help();
}

if ($i18nData->hasChanged()) {
	$i18nFile->dump($i18nData);
}

/**
 * Output help message.
 */
function help() {
	$help = <<<HELP
NAME
	%s

SYNOPSIS
	php %s [OPTION] [OPERATION] [KEY] [VALUE] [LANGUAGE]

DESCRIPTION
	Manipulate translation files. Available operations are 
	Check if translation files have missing keys or missing translations.

	-h	display this help and exit.

OPERATION
	add_language
		add a new language by duplicating the referential. This operation
		needs only a KEY.

	add_key	add a new key in the referential. This operation needs a KEY and
		a VALUE.

	add_value
		add a value in the referential. This operation needs a KEY, a
		VALUE, and a LANGUAGE.

	duplicate_key
		duplicate a referential key in other languages. This operation
		needs only a KEY.

	delete_key
		delete a referential key from all languages. This operation needs
		only a KEY.

	format  format i18n files.

HELP;
	$file = str_replace(__DIR__ . '/', '', __FILE__);
	echo sprintf($help, $file, $file);
	exit;
}
<commit_msg>Change code to be more robust<commit_after><?php

$options = getopt("a:hk:l:v:");

if (array_key_exists('h', $options)) {
	help();
}

if (!array_key_exists('a', $options)) {
	error('You need to specify the action to perform.');
}

require_once __DIR__ . '/i18n/I18nFile.php';

$i18nFile = new I18nFile();
$i18nData = $i18nFile->load();

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.');
		}
		break;
	case 'delete' :
		if (array_key_exists('k', $options)) {
			$i18nData->removeKey($options['k']);
		} else {
			error('You need to specify the key to delete.');
		}
		break;
	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);
		break;
	default :
		help();
}

if ($i18nData->hasChanged()) {
	$i18nFile->dump($i18nData);
}

/**
 * Output error message.
 */
function error($message) {
	$error = <<<ERROR
WARNING
	%s\n\n
ERROR;
	echo sprintf($error, $message);
	help();
}

/**
 * Output help message.
 */
function help() {
	$help = <<<HELP
NAME
	%1\$s

SYNOPSIS
	php %1\$s [OPTIONS]

DESCRIPTION
	Manipulate translation files.

	-a=ACTION
		select the action to perform. Available actions are add, delete,
		duplicate, and format. This option is mandatory.
	-k=KEY	select the key to work on.
	-v=VAL	select the value to set.
	-l=LANG	select the language to work on.
	-h	display this help and exit.

EXEMPLE
Exemple 1: add a language. It adds a new language by duplicating the referential.
	php %1\$s -a add -l my_lang

Exemple 2: add a new key. It adds the key in the referential.
	php %1\$s -a add -k my_key -v my_value

Exemple 3: add a new value. It adds a new value for the selected key in the selected language.
	php %1\$s -a add -k my_key -v my_value -l my_lang

Exemple 4: delete a key. It deletes the selected key in every languages.
	php %1\$s -a delete -k my_key

Exemple 5: duplicate a key. It duplicates the key from the referential in every languages.
	php %1\$s -a duplicate -k my_key

Exemple 6: format i18n files.
	php %1\$s -a format

HELP;
	$file = str_replace(__DIR__ . '/', '', __FILE__);
	echo sprintf($help, $file);
	exit;
}

{'fraction_non_alphanumeric': 0.15599820948970455, 'fraction_numerical': 0.02126230975828111, 'mean_word_length': 3.8628944504896627, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '751978', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1679, 'n_words': 504}