3 namespace pharext\Cli\Args
;
11 function __construct($prog, Args
$args) {
16 function __toString() {
17 $usage = "Usage:\n\n \$ ";
18 $usage .= $this->prog
;
20 list($flags, $required, $optional) = $this->listSpec();
22 $usage .= $this->dumpFlags($flags);
25 $usage .= $this->dumpRequired($required);
28 $usage .= $this->dumpOptional($optional);
31 $help = $this->dumpHelp();
33 return $usage . "\n\n" . $help . "\n";
40 foreach ($this->args
->getSpec() as $spec) {
41 if ($spec[3] & Args
::REQARG
) {
42 if ($spec[3] & Args
::REQUIRED
) {
52 return [$flags, $required, $optional] +
compact("flags", "required", "optional");
55 function dumpFlags(array $flags) {
56 return sprintf(" [-%s]", implode("", array_column($flags, 0)));
59 function dumpRequired(array $required) {
61 foreach ($required as $req) {
62 $dump .= sprintf(" -%s <%s>", $req[0], $req[1]);
67 function dumpOptional(array $optional) {
68 return sprintf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
71 function calcMaxLen() {
72 $spc = $this->args
->getSpec();
73 $max = max(array_map("strlen", array_column($spc, 1)));
79 $max = $this->calcMaxLen();
81 foreach ($this->args
->getSpec() as $spec) {
83 if (isset($spec[0])) {
84 $dump .= sprintf("-%s|", $spec[0]);
86 $dump .= sprintf("--%s ", $spec[1]);
87 if ($spec[3] & Args
::REQARG
) {
89 } elseif ($spec[3] & Args
::OPTARG
) {
95 $dump .= str_repeat(" ", $max-strlen($spec[1])+
3*!isset($spec[0]));
98 if ($spec[3] & Args
::REQUIRED
) {
99 $dump .= " (REQUIRED)";
101 if (isset($spec[4])) {
102 $dump .= sprintf(" [%s]", $spec[4]);