5 use pharext\Cli\Args
as CliArgs
;
9 if (!function_exists("array_column")) {
10 function array_column(array $array, $col, $idx = null) {
12 foreach ($array as $el) {
14 $result[$el[$idx]] = $el[$col];
16 $result[] = $el[$col];
26 * Command line arguments
27 * @var pharext\CliArgs
33 * @see \pharext\Command::getArgs()
35 public function getArgs() {
40 * Retrieve metadata of the currently running phar
44 public function metadata($key = null) {
45 $running = new Phar(Phar
::running(false));
47 if ($key === "signature") {
48 $sig = $running->getSignature();
49 return sprintf("%s signature of %s\n%s",
51 $this->metadata("name"),
52 chunk_split($sig["hash"], 64, "\n"));
55 $metadata = $running->getMetadata();
57 return $metadata[$key];
63 * Output pharext vX.Y.Z header
65 public function header() {
66 if (!headers_sent()) {
67 /* only display header, if we didn't generate any output yet */
68 printf("%s\n\n", $this->metadata("header"));
74 * @see \pharext\Command::debug()
76 public function debug($fmt) {
77 if ($this->args
->verbose
) {
78 vprintf($fmt, array_slice(func_get_args(), 1));
84 * @see \pharext\Command::info()
86 public function info($fmt) {
87 if (!$this->args
->quiet
) {
88 vprintf($fmt, array_slice(func_get_args(), 1));
94 * @see \pharext\Command::warn()
96 public function warn($fmt) {
97 if (!$this->args
->quiet
) {
100 $arg = error_get_last()["message"];
102 $arg = array_slice(func_get_args(), 1);
104 vfprintf(STDERR
, "Warning: $fmt", $arg);
110 * @see \pharext\Command::error()
112 public function error($fmt) {
115 $arg = error_get_last()["message"];
117 $arg = array_slice(func_get_args(), 1);
119 vfprintf(STDERR
, "ERROR: $fmt", $arg);
123 * Output command line help message
124 * @param string $prog
126 public function help($prog) {
127 printf("Usage:\n\n \$ %s", $prog);
132 foreach ($this->args
->getSpec() as $spec) {
133 if ($spec[3] & CliArgs
::REQARG
) {
134 if ($spec[3] & CliArgs
::REQUIRED
) {
145 printf(" [-%s]", implode("", array_column($flags, 0)));
147 foreach ($required as $req) {
148 printf(" -%s <arg>", $req[0]);
151 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
154 $spc = $this->args
->getSpec();
155 $max = max(array_map("strlen", array_column($spc, 1)));
156 $max +
= $max %
8 +
2;
157 foreach ($spc as $spec) {
158 if (isset($spec[0])) {
159 printf(" -%s|", $spec[0]);
163 printf("--%s ", $spec[1]);
164 if ($spec[3] & CliArgs
::REQARG
) {
166 } elseif ($spec[3] & CliArgs
::OPTARG
) {
171 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+
3*!isset($spec[0])), $spec[2]);
172 if ($spec[3] & CliArgs
::REQUIRED
) {
173 printf(" (REQUIRED)");
175 if (isset($spec[4])) {
176 printf(" [%s]", $spec[4]);
187 public function verbosity() {
188 if ($this->args
->verbose
) {
190 } elseif ($this->args
->quiet
) {