From: Michael Wallner Date: Mon, 18 May 2015 13:20:52 +0000 (+0200) Subject: add Cli\Args\Help X-Git-Tag: v4.0.0~4 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=605f3e81a9c0e1d16b2d22297569c1d164635c5c;p=pharext%2Fpharext add Cli\Args\Help --- diff --git a/Makefile b/Makefile index 41b8041..161d2de 100644 --- 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 ... " diff --git a/bin/pharext b/bin/pharext index 8f90155..0858fa6 100755 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 index 0000000..19e8cd4 --- /dev/null +++ b/src/pharext/Cli/Args/Help.php @@ -0,0 +1,108 @@ +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 ]", 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 .= " "; + } elseif ($spec[3] & Args::OPTARG) { + $dump .= "[]"; + } 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; + } +} diff --git a/src/pharext/Cli/Command.php b/src/pharext/Cli/Command.php index 6ad2c80..b733885 100644 --- a/src/pharext/Cli/Command.php +++ b/src/pharext/Cli/Command.php @@ -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 ", $req[0]); - } - if ($optional) { - printf(" [-%s ]", 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(" "); - } 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]); - } - printf("\n"); - } - printf("\n"); + print new Args\Help($prog, $this->args); } /**