1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php
if (PHP_SAPI !== 'CLI')
exit('-- CLI ONLY --');
// nom du script
$script = array_shift($argv);
$text['help'] = <<< EOF
{$script} [options] <file>
Ce que fait ton script
Options:
-h, --help Affiche ce message
-v, --verbose Passe en mode verbeux
-a Passe en mode tous
-tpl <template> Utiliser le template <template>
...
EOF;
// valeur des options par défaut
$options = array(
'verbose' => false,
'all' => false,
'template' => null,
);
// le dernier paramètre doit être <file>
$file = array_pop($argv);
if (empty($file)) {
echo "Aucun fichier fourni\n";
exit(-1);
}
if (!file_exists($file)) {
echo "$file n'existe pas\n";
exit(-1);
}
// parser les arguments
for ($i = 0, $cargs = count($argv); $i < $cargs; $i ++, $arg = $argv[$i]) {
switch ($arg) {
case '-h':
case '--help':
echo $text['help'];
exit(0);
case '-v':
case '--verbose':
$options['verbose'] = true;
break;
case '-a':
$options['all'] = true;
break;
case '-tpl':
$options['tpl'] = isset($argv[++$i]) ? $argv[$i] : null;
break;
// etc...
}
}
// on peut commencer le boulot
// .... |
Partager