X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FCliCommand.php;h=9ae9989391c8f46acb8553f3f12de071d5398b25;hb=50e5714c543ed50557a1b644c6df27b8d298b6e1;hp=1299a0e99397bb437fd9f58c308d5ccc8fd3b945;hpb=4e1839950dd39fd793346e724e4157bce8a169bf;p=pharext%2Fpharext diff --git a/src/pharext/CliCommand.php b/src/pharext/CliCommand.php index 1299a0e..9ae9989 100644 --- a/src/pharext/CliCommand.php +++ b/src/pharext/CliCommand.php @@ -12,19 +12,53 @@ trait CliCommand */ private $args; + /** + * @inheritdoc + * @see \pharext\Command::getArgs() + */ + public function getArgs() { + return $this->args; + } + /** * Output pharext vX.Y.Z header */ function header() { - printf("pharext v%s (c) Michael Wallner \n", VERSION); + printf("pharext v%s (c) Michael Wallner \n\n", VERSION); } + /** + * @inheritdoc + * @see \pharext\Command::info() + */ + public function info($fmt) { + if (!$this->args->quiet) { + vprintf($fmt, array_slice(func_get_args(), 1)); + } + } + + /** + * @inheritdoc + * @see \pharext\Command::error() + */ + public function error($fmt) { + if (!$this->args->quiet) { + if (!isset($fmt)) { + $fmt = "%s\n"; + $arg = error_get_last()["message"]; + } else { + $arg = array_slice(func_get_args(), 1); + } + vfprintf(STDERR, "ERROR: $fmt", $arg); + } + } + /** * Output command line help message * @param string $prog */ public function help($prog) { - printf("\nUsage:\n\n \$ %s", $prog); + printf("Usage:\n\n \$ %s", $prog); $flags = []; $required = []; @@ -51,9 +85,27 @@ trait CliCommand printf(" [-%s ]", implode("|-", array_column($optional, 0))); } printf("\n\n"); - foreach ($this->args->getSpec() as $spec) { - printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & CliArgs::REQARG) ? " " : (($spec[3] & CliArgs::OPTARG) ? "[]" : " ")); - printf("%s%s%s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & CliArgs::REQUIRED) ? " (REQUIRED)" : ""); + $spc = $this->args->getSpec(); + $max = max(array_map("strlen", array_column($spc, 1))); + $max += $max % 8 + 2; + foreach ($spc as $spec) { + if (isset($spec[0])) { + printf(" -%s|", $spec[0]); + } else { + printf(" "); + } + printf("--%s ", $spec[1]); + if ($spec[3] & CliArgs::REQARG) { + printf(" "); + } elseif ($spec[3] & CliArgs::OPTARG) { + printf("[]"); + } else { + printf(" "); + } + printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0])), $spec[2]); + if ($spec[3] & CliArgs::REQUIRED) { + printf(" (REQUIRED)"); + } if (isset($spec[4])) { printf(" [%s]", $spec[4]); } @@ -62,5 +114,50 @@ trait CliCommand printf("\n"); } - -} \ No newline at end of file + /** + * Create temporary file/directory name + * @param string $prefix + * @param string $suffix + */ + private function tempname($prefix, $suffix = null) { + if (!isset($suffix)) { + $suffix = uniqid(); + } + return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix); + } + + /** + * Create a new temp directory + * @param string $prefix + * @return string + */ + private function newtemp($prefix) { + $temp = $this->tempname($prefix); + if (!is_dir($temp)) { + if (!mkdir($temp, 0700, true)) { + $this->error(null); + exit(3); + } + } + return $temp; + } + + /** + * rm -r + * @param string $dir + */ + private function rm($dir) { + foreach (scandir($dir) as $entry) { + if ($entry === "." || $entry === "..") { + continue; + } elseif (is_dir("$dir/$entry")) { + $this->rm("$dir/$entry"); + } elseif (!unlink("$dir/$entry")) { + $this->error(null); + } + } + if (!rmdir($dir)) { + $this->error(null); + } + } +}