286 lines
10 KiB
PHP
286 lines
10 KiB
PHP
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
|
|
# fichier utilisé pour les tests. on peut y écrire du code pour vérifier le
|
|
# fonctionnement de certaines classes et méthodes
|
|
require(__DIR__.'/../vendor/autoload.php');
|
|
|
|
use nur\b\proc\AbstractCmd;
|
|
use nur\b\proc\Cmd;
|
|
use nur\b\proc\CmdAnd;
|
|
use nur\b\proc\CmdOr;
|
|
use nur\b\proc\CmdPipe;
|
|
use nur\cli\Application;
|
|
use nur\msg;
|
|
|
|
Application::run(new class extends Application {
|
|
const ARGS1 = [
|
|
"purpose" => "démontrer l'utilisation de ArgsParser",
|
|
|
|
["-a", "name" => "count", "help" => "Ajouter 1 au compteur"],
|
|
["-z", "name" => "count", "inverse" => true, "help" => "Retrancher 1 au compteur"],
|
|
["action1", "help" => "déclencher l'action n°1"],
|
|
|
|
"sections" => [
|
|
[
|
|
"title" => "Gestion des valeurs",
|
|
["-v", "name" => "fixed_value", "value" => 42, "help" => "Fixer la valeur à 42"],
|
|
["-s", "--set", "arg" => "value", "name" => "value", "help" => "Spécifier la valeur"],
|
|
["-d", "--destdir", "arg" => "path", "help" => "Spécifier le répertoire de destination"],
|
|
["-h", "--host", "arg" => "host", "help" => "Spécifier l'hôte sur lequel se connecter"],
|
|
["--inc", "action" => "--inc", "help" => "Ajouter 1 à la valeur"],
|
|
["--dec", "action" => "--dec", "help" => "Enlever 1 à la valeur"],
|
|
["--set", "arg" => "value", "action" => "--set", "help" => "Spécifier la valeur"],
|
|
["--add", "arg" => "value", "action" => "--add", "help" => "Ajouter une valeur au tableau"],
|
|
["--fix", "action" => "--set", "value" => 53, "help" => "Fixer la valeur à 53"],
|
|
],
|
|
[
|
|
"title" => "Arguments obligatoires et optionnels",
|
|
["--mm", "args" => ["value", "value"]],
|
|
["--mo", "args" => ["value", ["value"]]],
|
|
["--oo", "args" => [["value", "value"]]],
|
|
["--many", "args" => ["value", null]],
|
|
["--any", "args" => [null]],
|
|
["--mooany", "args" => ["value", ["value", "value", null]]],
|
|
],
|
|
[
|
|
"title" => "Actions",
|
|
["--honk", "action" => [self::class, "honk"], "help" => "Activer le klaxon"],
|
|
["--pouet", "action" => "->pouet", "help" => "Activer le klaxon"],
|
|
["--x2", "arg" => "value", "action" => "->x2",
|
|
"help" => "Multiplier la valeur par 2"]
|
|
],
|
|
[
|
|
"title" => "test Override",
|
|
["-x", "--longx1", "help" => "l'option -x ne devrait pas apparaitre ici parce qu'elle est réutilisée"],
|
|
["-x", "--longx2", "help" => "l'option -x devrait apparaitre ici"],
|
|
["action2", "help" => "déclencher l'action n°2"],
|
|
],
|
|
[
|
|
"title" => "test Group",
|
|
["group",
|
|
["--profile", "args" => "value", "name" => "profile",
|
|
"help" => "spécifier le profil",
|
|
],
|
|
["-P", "--prod", "name" => "profile", "value" => "prod",
|
|
"help" => "alias pour --profile prod",
|
|
],
|
|
["-T", "--test", "name" => "profile", "value" => "test",
|
|
"help" => "alias pour --profile test",
|
|
],
|
|
["--devel", "name" => "profile", "value" => "devel",
|
|
"help" => "alias pour --profile devel",
|
|
],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
private $count = 0;
|
|
private $value;
|
|
private $fixed_value;
|
|
private $destdir;
|
|
private $host;
|
|
private $inc;
|
|
private $dec = 100;
|
|
private $set;
|
|
private $add;
|
|
private $fix;
|
|
private $mm, $mo, $oo, $many, $any, $mooany;
|
|
private $profile;
|
|
function run1() {
|
|
Txx("Hello!");
|
|
Txx("count:", $this->count);
|
|
Txx("value:", $this->value);
|
|
Txx("fixed_value:", $this->fixed_value);
|
|
Txx("destdir:", $this->destdir);
|
|
Txx("host:", $this->host);
|
|
Txx("--inc:", $this->inc);
|
|
Txx("--dec:", $this->dec);
|
|
Txx("--set:", $this->set);
|
|
Txx("--add:", $this->add);
|
|
Txx("--fix:", $this->fix);
|
|
Txx("mm", $this->mm);
|
|
Txx("mo", $this->mo);
|
|
Txx("oo", $this->oo);
|
|
Txx("many", $this->many);
|
|
Txx("any", $this->any);
|
|
Txx("mooany", $this->mooany);
|
|
Txx("profile", $this->profile);
|
|
Txx("args:", $this->args);
|
|
}
|
|
static function honk() {
|
|
echo "honk honk!\n";
|
|
}
|
|
function pouet() {
|
|
echo "pouet pouet!\n";
|
|
}
|
|
function x2($value) {
|
|
$double = $value * 2;
|
|
echo "$value * 2 = $double\n";
|
|
}
|
|
|
|
#############################################################################
|
|
|
|
const ARGS2 = [
|
|
["-a", "help" => "option courte sans argument"],
|
|
["-b", "args" => "value", "help" => "option courte avec argument"],
|
|
["-al1", "help" => "option longue sans argument"],
|
|
["-bl1", "args" => "value", "help" => "option longue avec argument"],
|
|
["--al2", "help" => "option longue sans argument"],
|
|
["--bl2", "args" => "value", "help" => "option longue avec argument"],
|
|
["ac", "action" => "->doAc", "help" => "commande sans argument"],
|
|
["bc", "action" => "->doBc", "args" => "value", "help" => "commande avec argument"],
|
|
|
|
["c", "type" => "option", "help" => "option sans tiret"],
|
|
["d", "type" => "option", "args" => "value", "help" => "option sans tiret avec argument"],
|
|
|
|
["-e", "type" => "command", "action" => "->doE", "help" => "commande courte tiret simple sans argument"],
|
|
["-f", "type" => "command", "action" => "->doF", "args" => "value", "help" => "commande courte tiret simple avec argument"],
|
|
["-ec1", "type" => "command", "action" => "->doEc1", "help" => "commande longue tiret simple sans argument"],
|
|
["-fc1", "type" => "command", "action" => "->doFc1", "args" => "value", "help" => "commande longue tiret simple avec argument"],
|
|
["--ec2", "type" => "command", "action" => "->doEc2", "help" => "commande longue tiret double sans argument"],
|
|
["--fc2", "type" => "command", "action" => "->doFc2", "args" => "value", "help" => "commande longue tiret double avec argument"],
|
|
];
|
|
private $a, $b, $al1, $bl1, $al2, $bl2, $ac, $bc;
|
|
private $c, $d;
|
|
private $e, $f, $ec1, $fc1, $ec2, $fc2;
|
|
function run2() {
|
|
msg::section("option et commandes classiques");
|
|
Txx("a", $this->a);
|
|
Txx("b", $this->b);
|
|
Txx("al1", $this->al1);
|
|
Txx("bl1", $this->bl1);
|
|
Txx("al2", $this->al2);
|
|
Txx("bl2", $this->bl2);
|
|
Txx("ac", $this->ac);
|
|
Txx("bc", $this->bc);
|
|
msg::section("options sans tiret");
|
|
Txx("c", $this->c);
|
|
Txx("d", $this->d);
|
|
msg::section("commandes avec tiret");
|
|
Txx("e", $this->e);
|
|
Txx("f", $this->f);
|
|
Txx("ec1", $this->ec1);
|
|
Txx("fc1", $this->fc1);
|
|
Txx("ec2", $this->ec2);
|
|
Txx("fc2", $this->fc2);
|
|
}
|
|
function doAc($value, $name, $arg) {
|
|
Txx("ac!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->ac = 1;
|
|
}
|
|
function doBc($value, $name, $arg) {
|
|
Txx("bc!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->bc = $value;
|
|
}
|
|
function doE($value, $name, $arg) {
|
|
Txx("e!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->e = 1;
|
|
}
|
|
function doF($value, $name, $arg) {
|
|
Txx("f!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->f = $value;
|
|
}
|
|
function doEc1($value, $name, $arg) {
|
|
Txx("ec1!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->ec1 = 1;
|
|
}
|
|
function doFc1($value, $name, $arg) {
|
|
Txx("fc1!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->fc1 = $value;
|
|
}
|
|
function doEc2($value, $name, $arg) {
|
|
Txx("ec2!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->ec2 = 1;
|
|
}
|
|
function doFc2($value, $name, $arg) {
|
|
Txx("fc2!", "value", $value, "name", $name, "arg", $arg, "def");
|
|
$this->fc2 = $value;
|
|
}
|
|
|
|
#############################################################################
|
|
|
|
const ARGS3 = [
|
|
["--cc", "name" => "cmdClass", "value" => Cmd::class],
|
|
["--ca", "name" => "cmdClass", "value" => CmdAnd::class],
|
|
["--co", "name" => "cmdClass", "value" => CmdOr::class],
|
|
["--cp", "name" => "cmdClass", "value" => CmdPipe::class],
|
|
["-v", "--var", "args" => "value", "action" => "->addVar"],
|
|
["-V", "--lvar", "args" => "value", "action" => "->addLvar"],
|
|
["-a", "--add", "args" => "value", "action" => "->addCmd"],
|
|
["-A", "--ladd", "args" => "value", "action" => "->addLcmd"],
|
|
["-p", "--prefix", "args" => "value", "action" => "->addPrefix"],
|
|
["--xp", "name" => "action", "value" => "passthru"],
|
|
["--xs", "name" => "action", "value" => "system"],
|
|
["--xx", "name" => "action", "value" => "exec"],
|
|
["--xf", "name" => "action", "value" => "fork_exec"],
|
|
];
|
|
private $cmdClass = Cmd::class;
|
|
private $cmd;
|
|
private function cmd(): AbstractCmd {
|
|
if ($this->cmd === null) $this->cmd = new $this->cmdClass();
|
|
return $this->cmd;
|
|
}
|
|
private $vars;
|
|
function addVar($var) {
|
|
[$name, $value] = explode("=", $var);
|
|
$this->cmd()->addVars([$name => $value]);
|
|
}
|
|
function addLvar($var) {
|
|
$this->cmd()->addLiteralVars($var);
|
|
}
|
|
private $cmds;
|
|
function addCmd($cmd) {
|
|
$this->cmd()->add($cmd);
|
|
}
|
|
function addLcmd($cmd) {
|
|
$this->cmd()->addLiteral($cmd);
|
|
}
|
|
function addPrefix($prefix) {
|
|
$this->cmd()->addPrefix($prefix);
|
|
}
|
|
private $action;
|
|
|
|
function run3() {
|
|
$cmd = $this->cmd();
|
|
msg::info("command: |\n".$cmd->getCmd()."\n|");
|
|
switch ($this->action) {
|
|
case "passthru":
|
|
msg::section("passthru");
|
|
$cmd->passthru($retcode);
|
|
msg::info("retcode=$retcode");
|
|
break;
|
|
case "system":
|
|
msg::section("system");
|
|
$cmd->system($output,$retcode);
|
|
$output = var_export($output, true);
|
|
msg::info("output=|$output|, retcode=$retcode");
|
|
break;
|
|
case "exec":
|
|
msg::section("exec");
|
|
$cmd->exec($output, $retcode);
|
|
$output = var_export($output, true);
|
|
msg::info("output=|$output|, retcode=$retcode");
|
|
break;
|
|
case "fork_exec":
|
|
msg::section("fork_exec");
|
|
$cmd->fork_exec($retcode);
|
|
msg::info("retcode=$retcode");
|
|
break;
|
|
}
|
|
}
|
|
|
|
#############################################################################
|
|
|
|
const ARGS = self::ARGS1;
|
|
private $args;
|
|
|
|
function main() {
|
|
if (self::ARGS === self::ARGS1) {
|
|
$this->run1();
|
|
} elseif (self::ARGS === self::ARGS2) {
|
|
$this->run2();
|
|
} elseif (self::ARGS === self::ARGS3) {
|
|
$this->run3();
|
|
}
|
|
}
|
|
});
|