This commit is contained in:
Jephté Clain 2024-05-13 23:47:44 +04:00
parent 4494c8ecc3
commit 8d9b4d7949
3 changed files with 144 additions and 10 deletions

View File

@ -348,13 +348,16 @@ class ArgsParser {
$have_remains = false; $have_remains = false;
$have_help = false; $have_help = false;
$sections[] = $this->buildSection([], $defs, $meta, $sdefs, $ldefs, $cdefs, $rdef, $have_remains, $have_help);
if ($msections !== null) { if ($msections !== null) {
foreach ($msections as $section) { foreach ($msections as $msection) {
[$defs, $section] = A::split_assoc($section); [$mdefs, $msection] = A::split_assoc($msection);
$sections[] = $this->buildSection($section, $defs, $meta, $sdefs, $ldefs, $cdefs, $rdef, $have_remains, $have_help); $sections[] = $this->buildSection($msection, $mdefs, $meta, $sdefs, $ldefs, $cdefs, $rdef, $have_remains, $have_help);
} }
} }
# calculer la section par défaut à la fin, pour qu'elle puisse surcharger
# des options définies précédemment. par contre, il faut la mettre au début
# du tableau $sections
array_unshift($sections, $this->buildSection([], $defs, $meta, $sdefs, $ldefs, $cdefs, $rdef, $have_remains, $have_help));
if (!$have_remains && $autoremains) { if (!$have_remains && $autoremains) {
$def = [ $def = [

View File

@ -0,0 +1,39 @@
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
require(__DIR__.'/../../vendor/autoload.php');
use nur\cli\Application;
use nur\sery\output\msg;
class TestArgs4 extends Application {
protected $query;
function main() {
msg::info(["query=", $this->query]);
}
}
if (getenv("TA4") === "bad") {
TestArgs4::run(new class extends TestArgs4 {
const ARGS = [
"merge" => Application::ARGS,
# avant la modification du 13/05/2024
# la section par défaut est située AVANT les autres sections. les sections
# définies ont donc la priorité
# l'option -q ci-dessous est masquée par l'option -q,--quiet
["-q", "--query", "args" => "value"],
];
});
} else {
TestArgs4::run(new class extends TestArgs4 {
const ARGS = [
"merge" => Application::ARGS,
"sections" => [
[
"title" => "QUERY",
# en mettant l'option dans une autre section, l'ordre est respecté et
# donc -q n'est plus masqué
["-q", "--query", "args" => "value"],
],
],
];
});
}

View File

@ -243,20 +243,20 @@ class ArgsParserTest extends TestCase {
unset($dest); unset($dest);
self::parse($dest, $defs, []); self::parse($dest, $defs, []);
self::assertObjectNotHasAttribute("enable", $dest); self::assertObjectNotHasProperty("enable", $dest);
self::assertObjectHasAttribute("args", $dest); self::assertObjectHasProperty("args", $dest);
self::assertSame([], $dest->args); self::assertSame([], $dest->args);
unset($dest); unset($dest);
self::parse($dest, $defs, ["x"]); self::parse($dest, $defs, ["x"]);
self::assertObjectNotHasAttribute("enable", $dest); self::assertObjectNotHasProperty("enable", $dest);
self::assertObjectHasAttribute("args", $dest); self::assertObjectHasProperty("args", $dest);
self::assertSame(["x"], $dest->args); self::assertSame(["x"], $dest->args);
unset($dest); unset($dest);
self::parse($dest, $defs, ["x", "-a"]); self::parse($dest, $defs, ["x", "-a"]);
self::assertObjectHasAttribute("enable", $dest); self::assertObjectHasProperty("enable", $dest);
self::assertObjectHasAttribute("args", $dest); self::assertObjectHasProperty("args", $dest);
self::assertSame(1, $dest->enable); self::assertSame(1, $dest->enable);
self::assertSame(["x"], $dest->args); self::assertSame(["x"], $dest->args);
@ -579,4 +579,96 @@ class ArgsParserTest extends TestCase {
$parser->parse($dest, ["a", "b", "c"]); $parser->parse($dest, ["a", "b", "c"]);
self::assertSame(["a", "b", "c"], $dest->args); self::assertSame(["a", "b", "c"], $dest->args);
} }
function testOverride() {
$parser = new ArgsParser([
["-a", "--o1"],
["-a", "--o2"],
["-b", "--v1", "args" => 1],
["-b", "--v2", "args" => 1],
["-c", "--x1"],
["-c", "--x2", "args" => 1],
]);
$object = new stdClass();
$object->o1 = 0;
$object->o2 = 0;
$object->v1 = null;
$object->v2 = null;
$object->x1 = 0;
$object->x2 = null;
$parser->parse($object, ["-a", "-b", "valb", "-c", "valc"]);
self::assertSame(0, $object->o1);
self::assertSame(1, $object->o2);
self::assertNull($object->v1);
self::assertSame("valb", $object->v2);
self::assertSame(0, $object->x1);
self::assertSame("valc", $object->x2);
$parser = new ArgsParser([
"sections" => [
[
["-a", "--o1"],
["-b", "--v1", "args" => 1],
["-c", "--x1"],
],
[
["-a", "--o2"],
["-b", "--v2", "args" => 1],
["-c", "--x2", "args" => 1],
],
],
]);
$object = new stdClass();
$object->o1 = 0;
$object->o2 = 0;
$object->v1 = null;
$object->v2 = null;
$object->x1 = 0;
$object->x2 = null;
$parser->parse($object, ["-a", "-b", "valb", "-c", "valc"]);
self::assertSame(0, $object->o1);
self::assertSame(1, $object->o2);
self::assertNull($object->v1);
self::assertSame("valb", $object->v2);
self::assertSame(0, $object->x1);
self::assertSame("valc", $object->x2);
$parser = new ArgsParser([
"merge" => [
"sections" => [
[
"title" => "une section",
["group",
["-A", "--g1"],
["-a", "--g2"],
],
],
],
],
["-a", "--o1"],
["-b", "--v1", "args" => 1],
["-c", "--x1"],
["-a", "--o2"],
["-b", "--v2", "args" => 1],
["-c", "--x2", "args" => 1],
]);
$object = new stdClass();
$object->g1 = 0;
$object->g2 = 0;
$object->o1 = 0;
$object->o2 = 0;
$object->v1 = null;
$object->v2 = null;
$object->x1 = 0;
$object->x2 = null;
$parser->parse($object, ["-a", "-b", "valb", "-c", "valc"]);
self::assertSame(0, $object->g1);
self::assertSame(0, $object->g2);
self::assertSame(0, $object->o1);
self::assertSame(1, $object->o2);
self::assertNull($object->v1);
self::assertSame("valb", $object->v2);
self::assertSame(0, $object->x1);
self::assertSame("valc", $object->x2);
}
} }