0ec52fffea07b06b829ee4e3ccffd68a651c8a98
5 use pharext\Cli\Args
as CliArgs
;
7 require_once "pharext/Version.php";
12 * Command line arguments
13 * @var pharext\CliArgs
19 * @see \pharext\Command::getArgs()
21 public function getArgs() {
26 * Output pharext vX.Y.Z header
29 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n",
35 * @see \pharext\Command::info()
37 public function info($fmt) {
38 if (!$this->args
->quiet
) {
39 vprintf($fmt, array_slice(func_get_args(), 1));
45 * @see \pharext\Command::error()
47 public function error($fmt) {
48 if (!$this->args
->quiet
) {
51 $arg = error_get_last()["message"];
53 $arg = array_slice(func_get_args(), 1);
55 vfprintf(STDERR
, "ERROR: $fmt", $arg);
60 * Output command line help message
63 public function help($prog) {
64 printf("Usage:\n\n \$ %s", $prog);
69 foreach ($this->args
->getSpec() as $spec) {
70 if ($spec[3] & CliArgs
::REQARG
) {
71 if ($spec[3] & CliArgs
::REQUIRED
) {
82 printf(" [-%s]", implode("", array_column($flags, 0)));
84 foreach ($required as $req) {
85 printf(" -%s <arg>", $req[0]);
88 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
91 $spc = $this->args
->getSpec();
92 $max = max(array_map("strlen", array_column($spc, 1)));
94 foreach ($spc as $spec) {
95 if (isset($spec[0])) {
96 printf(" -%s|", $spec[0]);
100 printf("--%s ", $spec[1]);
101 if ($spec[3] & CliArgs
::REQARG
) {
103 } elseif ($spec[3] & CliArgs
::OPTARG
) {
108 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+
3*!isset($spec[0])), $spec[2]);
109 if ($spec[3] & CliArgs
::REQUIRED
) {
110 printf(" (REQUIRED)");
112 if (isset($spec[4])) {
113 printf(" [%s]", $spec[4]);
121 * Create temporary file/directory name
122 * @param string $prefix
123 * @param string $suffix
125 private function tempname($prefix, $suffix = null) {
126 if (!isset($suffix)) {
129 return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix);
133 * Create a new temp directory
134 * @param string $prefix
137 private function newtemp($prefix) {
138 $temp = $this->tempname($prefix);
139 if (!is_dir($temp)) {
140 if (!mkdir($temp, 0700, true)) {
152 private function rm($dir) {
153 foreach (scandir($dir) as $entry) {
154 if ($entry === "." ||
$entry === "..") {
156 } elseif (is_dir("$dir/$entry")) {
157 $this->rm("$dir/$entry");
158 } elseif (!unlink("$dir/$entry")) {