fix command help text output
[pharext/pharext] / src / pharext / Cli / Args / Help.php
index 4284dbb500db5f54259e2e172836edc676545153..907a40d9aeb6959bec53de071034a7a65e556c95 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace pharext\Cli\Args;
 
+use function array_column;
 use pharext\Cli\Args;
 
 class Help
@@ -79,25 +80,66 @@ class Help
 
                $dump = "";
                if ($req) {
-                       $dump .= sprintf(" [-%s <arg>]", implode("|-", array_column($req, 0)));
+                       $short = array_filter($req, function($a) {
+                               return is_string($a[0]);
+                       });
+                       if ($short) {
+                               $dump .= sprintf(" [-%s <arg>]", implode("|-", array_column($short, 0)));
+                       }
+                       $long = array_filter($req, function($a) {
+                               return !is_string($a[0]);
+                       });
+                       if ($long) {
+                               $dump .= sprintf(" [--%s <arg>]", implode("|--", array_column($long, 1)));
+                       }
                }
                if ($opt) {
-                       $dump .= sprintf(" [-%s [<arg>]]", implode("|-", array_column($opt, 0)));
+                       $short = array_filter($opt, function($a) {
+                               return is_string($a[0]);
+                       });
+                       if ($short) {
+                               $dump .= sprintf(" [-%s [<arg>]]", implode("|-", array_column($short, 0)));
+                       }
+                       $long = array_filter($opt, function($a) {
+                               return !is_string($a[0]);
+                       });
+                       if ($long) {
+                               $dump .= sprintf(" [--%s [<arg>]]", implode("|--", array_column($long, 1)));
+                       }
                }
                return $dump;
        }
 
        function dumpPositional(array $positional) {
                $dump = " [--]";
+               $conv = [];
                foreach ($positional as $pos) {
-                       if ($pos[3] & Args::REQUIRED) {
-                               $dump .= sprintf(" <%s>", $pos[1]);
+                       $conv[$pos[0]][] = $pos;
+               }
+               $opts = [];
+               foreach ($conv as $positional) {
+                       $args = implode("|", array_column($positional, 1));
+                       if ($positional[0][3] & Args::REQUIRED) {
+                               $dump .= sprintf(" <%s>", $args);
                        } else {
-                               $dump .= sprintf(" [<%s>]", $pos[1]);
+                               $dump .= sprintf(" [<%s>]", $args);
+                       }
+                       if ($positional[0][3] & Args::MULTI) {
+                               $dump .= sprintf(" [<%s>]...", $args);
                        }
-                       if ($pos[3] & Args::MULTI) {
-                               $dump .= sprintf(" [<%s>]...", $pos[1]);
+                       /*
+                       foreach ($positional as $pos) {
+                               if ($pos[3] & Args::REQUIRED) {
+                                       $dump .= sprintf(" <%s>", $pos[1]);
+                               } else {
+                                       $opts[] = $pos;
+                                       //$dump .= sprintf(" [<%s>]", $pos[1]);
+                               }
+                               if ($pos[3] & Args::MULTI) {
+                                       $dump .= sprintf(" [<%s>]...", $pos[1]);
+                               }
                        }
+                        */
                }
                return $dump;
        }
@@ -115,7 +157,7 @@ class Help
                foreach ($this->args->getSpec() as $spec) {
                        $dump .= "    ";
                        if (is_numeric($spec[0])) {
-                               $dump .= sprintf("--   %s ", $spec[1]);
+                               $dump .= sprintf("  <%s>  ", $spec[1]);
                        } elseif (isset($spec[0])) {
                                $dump .= sprintf("-%s|", $spec[0]);
                        }
@@ -130,8 +172,9 @@ class Help
                                $dump .= "       ";
                        }
 
-                       $dump .= str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0]));
-                       $dump .= $spec[2];
+                       $space = str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0]));
+                       $dump .= $space;
+                       $dump .= str_replace("\n", "\n                        $space", $spec[2]);
 
                        if ($spec[3] & Args::REQUIRED) {
                                $dump .= " (REQUIRED)";