improve help output
authorMichael Wallner <mike@php.net>
Thu, 3 Sep 2015 11:25:10 +0000 (13:25 +0200)
committerMichael Wallner <mike@php.net>
Thu, 3 Sep 2015 11:25:10 +0000 (13:25 +0200)
bin/pharext
bin/pharext.update
src/pharext/Cli/Args/Help.php
tests/src/pharext/CliCommandTest.php

index 62ee132e705ef2a2da1b85a4e3eeae441dbf8c2b..85be95c57424dd4d6cd79e979424bc27e78f1af1 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
index f419b46da9c30040a56b99d5ffc5e9514ec9bde9..1a662842c9bd89fca82b38fd63a194713cff4145 100755 (executable)
Binary files a/bin/pharext.update and b/bin/pharext.update differ
index 50883b1773616778f63b17b43b884fb323ee6b1d..4284dbb500db5f54259e2e172836edc676545153 100644 (file)
@@ -44,12 +44,10 @@ class Help
                foreach ($this->args->getSpec() as $spec) {
                        if (is_numeric($spec[0])) {
                                $positional[] = $spec;
-                       } elseif ($spec[3] & Args::REQARG) {
-                               if ($spec[3] & Args::REQUIRED) {
-                                       $required[] = $spec;
-                               } else {
-                                       $optional[] = $spec;
-                               }
+                       } elseif ($spec[3] & Args::REQUIRED) {
+                               $required[] = $spec;
+                       } elseif ($spec[3] & (Args::OPTARG|Args::REQARG)) {
+                               $optional[] = $spec;
                        } else {
                                $flags[] = $spec;
                        }
@@ -72,21 +70,33 @@ class Help
        }
 
        function dumpOptional(array $optional) {
-               return sprintf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
+               $req = array_filter($optional, function($a) {
+                       return $a[3] & Args::REQARG;
+               });
+               $opt = array_filter($optional, function($a) {
+                       return $a[3] & Args::OPTARG;
+               });
+
+               $dump = "";
+               if ($req) {
+                       $dump .= sprintf(" [-%s <arg>]", implode("|-", array_column($req, 0)));
+               }
+               if ($opt) {
+                       $dump .= sprintf(" [-%s [<arg>]]", implode("|-", array_column($opt, 0)));
+               }
+               return $dump;
        }
 
        function dumpPositional(array $positional) {
                $dump = " [--]";
                foreach ($positional as $pos) {
-                       if ($pos[3] & Args::MULTI) {
-                               $multi = " ...";
-                       } else {
-                               $multi = "";
-                       }
                        if ($pos[3] & Args::REQUIRED) {
-                               $dump .= sprintf(" <%s%s>", $pos[1], $multi);
+                               $dump .= sprintf(" <%s>", $pos[1]);
                        } else {
-                               $dump .= sprintf(" [%s%s]", $pos[1], $multi);
+                               $dump .= sprintf(" [<%s>]", $pos[1]);
+                       }
+                       if ($pos[3] & Args::MULTI) {
+                               $dump .= sprintf(" [<%s>]...", $pos[1]);
                        }
                }
                return $dump;
@@ -101,7 +111,6 @@ class Help
 
        function dumpHelp() {
                $max = $this->calcMaxLen();
-               $parg = "";
                $dump = "";
                foreach ($this->args->getSpec() as $spec) {
                        $dump .= "    ";
index e4f80a946bceb5ef19766de2ebee4eec7070bcc5..f8f4da442ea9d2ad58294bb77fdd37bbb0a2dd3a 100644 (file)
@@ -42,7 +42,7 @@ class CliCommandTest extends \PHPUnit_Framework_TestCase
                $this->expectOutputString(<<<EOF
 Usage:
 
-  $ testprog [-hvqs] [-p|-n|-c <arg>]
+  $ testprog [-hvq] [-p|-n|-c <arg>] [-s [<arg>]]
 
     -h|--help                    Display help
     -v|--verbose                 More output