add Cli\Args\Help
authorMichael Wallner <mike@php.net>
Mon, 18 May 2015 13:20:52 +0000 (15:20 +0200)
committerMichael Wallner <mike@php.net>
Mon, 18 May 2015 13:20:52 +0000 (15:20 +0200)
Makefile
bin/pharext
src/pharext/Cli/Args/Help.php [new file with mode: 0644]
src/pharext/Cli/Command.php

index 41b8041305a66b4df0e894173db7312bb87c0e29..161d2deaa6d68bc20df3ce1455d79cac49e4d823 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@
 
 all: bin/pharext
 
-bin/pharext: src/* src/pharext/* src/pharext/*/*
+bin/pharext: src/* src/pharext/* src/pharext/*/* src/pharext/*/*/*
        @echo "Linting changed source files ... "
        @for file in $?; do php -l $$file | sed -ne '/^No syntax errors/!p' && exit $${PIPESTATUS[0]}; done
        @echo "Creating bin/pharext ... "
index 8f90155d7cc18f523256040d4e7df0eba2315e07..0858fa6cda382d7d846bfd179b2ebe0305f5f249 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
diff --git a/src/pharext/Cli/Args/Help.php b/src/pharext/Cli/Args/Help.php
new file mode 100644 (file)
index 0000000..19e8cd4
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+namespace pharext\Cli\Args;
+
+use pharext\Cli\Args;
+
+class Help
+{
+       private $args;
+
+       function __construct($prog, Args $args) {
+               $this->prog = $prog;
+               $this->args = $args;
+       }
+
+       function __toString() {
+               $usage = "Usage:\n\n  \$ ";
+               $usage .= $this->prog;
+
+               list($flags, $required, $optional) = $this->listSpec();
+               if ($flags) {
+                       $usage .= $this->dumpFlags($flags);
+               }
+               if ($required) {
+                       $usage .= $this->dumpRequired($required);
+               }
+               if ($optional) {
+                       $usage .= $this->dumpOptional($optional);
+               }
+
+               $help = $this->dumpHelp();
+               
+               return $usage . "\n\n" . $help . "\n";
+       }
+
+       function listSpec() {
+               $flags = [];
+               $required = [];
+               $optional = [];
+               foreach ($this->args->getSpec() as $spec) {
+                       if ($spec[3] & Args::REQARG) {
+                               if ($spec[3] & Args::REQUIRED) {
+                                       $required[] = $spec;
+                               } else {
+                                       $optional[] = $spec;
+                               }
+                       } else {
+                               $flags[] = $spec;
+                       }
+               }
+
+               return [$flags, $required, $optional] + compact("flags", "required", "optional");
+       }
+
+       function dumpFlags(array $flags) {
+               return sprintf(" [-%s]", implode("", array_column($flags, 0)));
+       }
+
+       function dumpRequired(array $required) {
+               $dump = "";
+               foreach ($required as $req) {
+                       $dump .= sprintf(" -%s <%s>", $req[0], $req[1]);
+               }
+               return $dump;
+       }
+
+       function dumpOptional(array $optional) {
+               return sprintf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
+       }
+
+       function calcMaxLen() {
+               $spc = $this->args->getSpec();
+               $max = max(array_map("strlen", array_column($spc, 1)));
+               $max += $max % 8 + 2;
+               return $max;
+       }
+
+       function dumpHelp() {
+               $max = $this->calcMaxLen();
+               $dump = "";
+               foreach ($this->args->getSpec() as $spec) {
+                       $dump .= "    ";
+                       if (isset($spec[0])) {
+                               $dump .= sprintf("-%s|", $spec[0]);
+                       }
+                       $dump .= sprintf("--%s ", $spec[1]);
+                       if ($spec[3] & Args::REQARG) {
+                               $dump .= "<arg>  ";
+                       } elseif ($spec[3] & Args::OPTARG) {
+                               $dump .= "[<arg>]";
+                       } else {
+                               $dump .= "       ";
+                       }
+
+                       $dump .= str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0]));
+                       $dump .= $spec[2];
+
+                       if ($spec[3] & Args::REQUIRED) {
+                               $dump .= " (REQUIRED)";
+                       }
+                       if (isset($spec[4])) {
+                               $dump .= sprintf(" [%s]", $spec[4]);
+                       }
+                       $dump .= "\n";
+               }
+               return $dump;
+       }
+}
index 6ad2c806b4a4ccabe3602f1b5ed8c836e5ca6423..b733885a26a9c39e52d1d15544a2a631b2e889ce 100644 (file)
@@ -124,60 +124,7 @@ trait Command
         * @param string $prog
         */
        public function help($prog) {
-               printf("Usage:\n\n  \$ %s", $prog);
-               
-               $flags = [];
-               $required = [];
-               $optional = [];
-               foreach ($this->args->getSpec() as $spec) {
-                       if ($spec[3] & CliArgs::REQARG) {
-                               if ($spec[3] & CliArgs::REQUIRED) {
-                                       $required[] = $spec;
-                               } else {
-                                       $optional[] = $spec;
-                               }
-                       } else {
-                               $flags[] = $spec;
-                       }
-               }
-       
-               if ($flags) {
-                       printf(" [-%s]", implode("", array_column($flags, 0)));
-               }
-               foreach ($required as $req) {
-                       printf(" -%s <arg>", $req[0]);
-               }
-               if ($optional) {
-                       printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
-               }
-               printf("\n\n");
-               $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("<arg>  ");
-                       } elseif ($spec[3] & CliArgs::OPTARG) {
-                               printf("[<arg>]");
-                       } 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]);
-                       }
-                       printf("\n");
-               }
-               printf("\n");
+               print new Args\Help($prog, $this->args);
        }
        
        /**