1299a0e99397bb437fd9f58c308d5ccc8fd3b945
[pharext/pharext] / src / pharext / CliCommand.php
1 <?php
2
3 namespace pharext;
4
5 require_once __DIR__."/Version.php";
6
7 trait CliCommand
8 {
9 /**
10 * Command line arguments
11 * @var pharext\CliArgs
12 */
13 private $args;
14
15 /**
16 * Output pharext vX.Y.Z header
17 */
18 function header() {
19 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n", VERSION);
20 }
21
22 /**
23 * Output command line help message
24 * @param string $prog
25 */
26 public function help($prog) {
27 printf("\nUsage:\n\n \$ %s", $prog);
28
29 $flags = [];
30 $required = [];
31 $optional = [];
32 foreach ($this->args->getSpec() as $spec) {
33 if ($spec[3] & CliArgs::REQARG) {
34 if ($spec[3] & CliArgs::REQUIRED) {
35 $required[] = $spec;
36 } else {
37 $optional[] = $spec;
38 }
39 } else {
40 $flags[] = $spec;
41 }
42 }
43
44 if ($flags) {
45 printf(" [-%s]", implode("", array_column($flags, 0)));
46 }
47 foreach ($required as $req) {
48 printf(" -%s <arg>", $req[0]);
49 }
50 if ($optional) {
51 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
52 }
53 printf("\n\n");
54 foreach ($this->args->getSpec() as $spec) {
55 printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & CliArgs::REQARG) ? "<arg> " : (($spec[3] & CliArgs::OPTARG) ? "[<arg>]" : " "));
56 printf("%s%s%s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & CliArgs::REQUIRED) ? " (REQUIRED)" : "");
57 if (isset($spec[4])) {
58 printf(" [%s]", $spec[4]);
59 }
60 printf("\n");
61 }
62 printf("\n");
63 }
64
65
66 }