nur-sery/wip/app/args.php

40 lines
1.0 KiB
PHP
Raw Normal View History

2024-09-26 10:54:43 +04:00
<?php
namespace nur\sery\wip\app;
use nur\sery\A;
use nur\sery\str;
class args {
/**
* transformer une liste d'argument de la forme
* - ["myArg" => $value] devient ["--my-arg", "$value"]
* - ["myOpt" => true] devient ["--my-opt"]
* - ["myOpt" => false] est omis
* - les autres valeurs sont prises telles quelles
*/
static function from_array(?array $array): array {
$args = [];
if ($array === null) return $args;
$index = 0;
foreach ($array as $arg => $value) {
if ($value === false) continue;
if ($arg === $index) {
$index++;
} else {
$arg = str::us2camel($arg);
$arg = str::camel2us($arg, false, "-");
$arg = str_replace("_", "-", $arg);
$args[] = "--$arg";
if (is_array($value)) $value[] = "--";
elseif ($value === true) $value = null;
}
if (is_array($value)) {
A::merge($args, array_map("strval", $value));
} elseif ($value !== null) {
$args[] = "$value";
}
}
return $args;
}
}