e19ce93a444ffd3d4d87a2be90ed589f897e86a8
[pharext/pharext] / src / pharext / Cli / Command.php
1 <?php
2
3 namespace pharext\Cli;
4
5 use pharext\Cli\Args as CliArgs;
6
7 require_once "pharext/Version.php";
8
9 trait Command
10 {
11 /**
12 * Command line arguments
13 * @var pharext\CliArgs
14 */
15 private $args;
16
17 /**
18 * @inheritdoc
19 * @see \pharext\Command::getArgs()
20 */
21 public function getArgs() {
22 return $this->args;
23 }
24
25 /**
26 * Output pharext vX.Y.Z header
27 */
28 function header() {
29 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n",
30 \pharext\VERSION);
31 }
32
33 /**
34 * @inheritdoc
35 * @see \pharext\Command::debug()
36 */
37 public function debug($fmt) {
38 if ($this->args->verbose) {
39 vprintf($fmt, array_slice(func_get_args(), 1));
40 }
41 }
42
43 /**
44 * @inheritdoc
45 * @see \pharext\Command::info()
46 */
47 public function info($fmt) {
48 if (!$this->args->quiet) {
49 vprintf($fmt, array_slice(func_get_args(), 1));
50 }
51 }
52
53 /**
54 * @inheritdoc
55 * @see \pharext\Command::error()
56 */
57 public function error($fmt) {
58 if (!$this->args->quiet) {
59 if (!isset($fmt)) {
60 $fmt = "%s\n";
61 $arg = error_get_last()["message"];
62 } else {
63 $arg = array_slice(func_get_args(), 1);
64 }
65 vfprintf(STDERR, "ERROR: $fmt", $arg);
66 }
67 }
68
69 /**
70 * Output command line help message
71 * @param string $prog
72 */
73 public function help($prog) {
74 printf("Usage:\n\n \$ %s", $prog);
75
76 $flags = [];
77 $required = [];
78 $optional = [];
79 foreach ($this->args->getSpec() as $spec) {
80 if ($spec[3] & CliArgs::REQARG) {
81 if ($spec[3] & CliArgs::REQUIRED) {
82 $required[] = $spec;
83 } else {
84 $optional[] = $spec;
85 }
86 } else {
87 $flags[] = $spec;
88 }
89 }
90
91 if ($flags) {
92 printf(" [-%s]", implode("", array_column($flags, 0)));
93 }
94 foreach ($required as $req) {
95 printf(" -%s <arg>", $req[0]);
96 }
97 if ($optional) {
98 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
99 }
100 printf("\n\n");
101 $spc = $this->args->getSpec();
102 $max = max(array_map("strlen", array_column($spc, 1)));
103 $max += $max % 8 + 2;
104 foreach ($spc as $spec) {
105 if (isset($spec[0])) {
106 printf(" -%s|", $spec[0]);
107 } else {
108 printf(" ");
109 }
110 printf("--%s ", $spec[1]);
111 if ($spec[3] & CliArgs::REQARG) {
112 printf("<arg> ");
113 } elseif ($spec[3] & CliArgs::OPTARG) {
114 printf("[<arg>]");
115 } else {
116 printf(" ");
117 }
118 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0])), $spec[2]);
119 if ($spec[3] & CliArgs::REQUIRED) {
120 printf(" (REQUIRED)");
121 }
122 if (isset($spec[4])) {
123 printf(" [%s]", $spec[4]);
124 }
125 printf("\n");
126 }
127 printf("\n");
128 }
129
130 /**
131 * rm -r
132 * @param string $dir
133 */
134 private function rm($dir) {
135 foreach (scandir($dir) as $entry) {
136 if ($entry === "." || $entry === "..") {
137 continue;
138 } elseif (is_dir("$dir/$entry")) {
139 $this->rm("$dir/$entry");
140 } elseif (!unlink("$dir/$entry")) {
141 $this->error(null);
142 }
143 }
144 if (!rmdir($dir)) {
145 $this->error(null);
146 }
147 }
148 }