diff --git a/Tests/test.php b/Tests/test.php index f0673ec..4f821a8 100644 --- a/Tests/test.php +++ b/Tests/test.php @@ -10,10 +10,10 @@ use CommandLine\CommandLine; $cmdline = new CommandLine('Test', 'Programme de test', 'php test.php'); $cmdline->addDefaultArguments(); -$cmdline->addOption(new Flag('array_form', false, 'Sous la forme d\'un tableau ?')); +$cmdline->addOption(new Flag('array_form', false, 'Sous la forme d\'un tableau ?'."\n".'Ou pas')); $cmdline->addOption((new Value('days', 'Nombre jour', new IntegerParser(0, 365)))->setDefault(3)); $cmdline->addOption((new Value('years', 'Nombre d\'années', new IntegerParser(0)))->setDefault(5)); -$cmdline->addValue(new \CommandLine\Argument\Value\Value('path', 'Chemin sauvegarde images', new StringParser(), true)); +$cmdline->addValue((new \CommandLine\Argument\Value\Value('path', 'Chemin sauvegarde images', new StringParser(), true))); $args = $cmdline->parseExplicit(array('--help')); $cmdline->treatDefaultArguments($args, false); diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..ab220b9 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "879579c4f4d897535fbe43c7d1a38fc1", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": "^5.6 || ^7.0" + }, + "platform-dev": [] +} diff --git a/src/CommandLine/CommandLine.php b/src/CommandLine/CommandLine.php index 26582c8..7a313ad 100644 --- a/src/CommandLine/CommandLine.php +++ b/src/CommandLine/CommandLine.php @@ -688,6 +688,7 @@ class CommandLine { $pads->name = 0; $pads->occurMin = 0; $pads->occurMax = 0; + $pads->default = 0; $pads->valueDescription = 0; // Lecture des arguments @@ -698,17 +699,30 @@ class CommandLine { $pads->occurMin = max($pads->occurMin, strlen($value->getOccurMin())); $pads->occurMax = max($pads->occurMax, strlen(is_null($max) ? 'N' : $max)); + $pads->default = max($pads->default, strlen($value->getDefault())); if ($value instanceof IArgumentValueDescription) { $pads->valueDescription = max($pads->valueDescription, strlen($value->getValueDescription())); } } + $spaces = array(); + $spaces[] = str_pad('', $pads->name, ' '); + $spaces[] = str_pad('', $pads->occurMin + 4 + $pads->occurMax, ' '); + if($pads->valueDescription > 0) { + $spaces[] = str_pad('', $pads->valueDescription, ' '); + } + if($pads->default > 0) { + $spaces[] = str_pad('', $pads->default, ' '); + } + $spaces[] = ''; + /* * Génération des descriptifs */ $entries = array(); foreach ($values as $value) { - $label = str_pad($value->getName(), $pads->name, ' ', STR_PAD_RIGHT); + $entry = array(); + $entry[] = str_pad($value->getName(), $pads->name, ' ', STR_PAD_RIGHT); $max = $value->getOccurMax(); @@ -716,35 +730,27 @@ class CommandLine { $occur .= str_pad($value->getOccurMin(), $pads->occurMin, ' ', STR_PAD_LEFT); $occur .= ' => '; $occur .= str_pad(is_null($max) ? 'N' : $max, $pads->occurMax, ' ', STR_PAD_RIGHT); + $entry[] = $occur; + + if($pads->valueDescription > 0) { + $entry[] = str_pad( + $value instanceof IArgumentValueDescription ? $value->getValueDescription() : '', + $pads->valueDescription, + ' ', + STR_PAD_RIGHT + ); + } + if ($pads->default > 0) { + $entry[] = str_pad($value->getDefault(), $pads->default, ' ', STR_PAD_RIGHT); + } - $valueDescription = str_pad( - $value instanceof IArgumentValueDescription ? $value->getValueDescription() : '', - $pads->valueDescription, - ' ', - STR_PAD_RIGHT + $entry[] = preg_replace( + '@\r?\n@', + '$0' . self::TAB . implode(self::TAB, $spaces), + $value->getDescription() ); - $entries[] = self::TAB . implode( - self::TAB, - array( - $label, - $occur, - $valueDescription, - preg_replace( - '@\r?\n@', - '$0' . self::TAB . implode( - self::TAB, - array( - str_pad('', $pads->name, ' '), - str_pad('', $pads->occurMin + 4 + $pads->occurMax, ' '), - str_pad('', $pads->valueDescription, ' '), - '', - ) - ), - $value->getDescription() - ), - ) - ); + $entries[] = self::TAB . implode(self::TAB, $entry); } return $entries; @@ -764,6 +770,7 @@ class CommandLine { $pads = new stdClass; $pads->tagShort = 0; $pads->tagLong = 0; + $pads->default = 0; $pads->valueDescription = 0; // Lecture des arguments @@ -774,6 +781,7 @@ class CommandLine { } $pads->tagLong = max($pads->tagLong, strlen($option->getTagLong())); + $pads->default = max($pads->default, strlen($option->getDefault())); if ($option instanceof IArgumentValueDescription) { $pads->valueDescription = max($pads->valueDescription, strlen($option->getValueDescription())); } @@ -783,11 +791,23 @@ class CommandLine { $pads->tagShort += 1; $pads->tagLong += 2; + $spaces = array(); + $spaces[] = str_pad('', $pads->tagShort + 1 + $pads->tagLong + 2, ' '); + if($pads->valueDescription > 0) { + $spaces[] = str_pad('', $pads->valueDescription, ' '); + } + if($pads->default > 0) { + $spaces[] = str_pad('', $pads->default, ' '); + } + $spaces[] = ''; + /* * Génération des descriptifs */ $entries = array(); foreach ($options as $option) { + $entry = array(); + $short = $option->getTagShort(); $label = ''; @@ -795,33 +815,28 @@ class CommandLine { $label .= ' '; $label .= str_pad('--' . $option->getTagLong(), $pads->tagLong, ' ', STR_PAD_RIGHT); $label .= $option->allowMultiple() ? ' *' : ($option->isStoppingParse() ? ' X' : ' '); + $entry[] = $label; + + if($pads->valueDescription) { + $entry[] = str_pad( + $option instanceof IArgumentValueDescription ? $option->getValueDescription() : '', + $pads->valueDescription, + ' ', + STR_PAD_RIGHT + ); + } + + if($pads->default > 0) { + $entry[] = str_pad($option->getDefault(), $pads->default, ' ', STR_PAD_RIGHT); + } - $valueDescription = str_pad( - $option instanceof IArgumentValueDescription ? $option->getValueDescription() : '', - $pads->valueDescription, - ' ', - STR_PAD_RIGHT + $entry[] = preg_replace( + '@\r?\n@', + '$0' . self::TAB . implode(self::TAB, $spaces), + $option->getDescription() ); - $entries[] = self::TAB . implode( - self::TAB, - array( - $label, - $valueDescription, - preg_replace( - '@\r?\n@', - '$0' . self::TAB . self::TAB . implode( - self::TAB, - array( - str_pad('', $pads->tagShort + 1 + $pads->tagLong + 2, ' '), - str_pad('', $pads->valueDescription, ' '), - '', - ) - ), - $option->getDescription() - ), - ) - ); + $entries[] = self::TAB . implode(self::TAB, $entry); } return $entries;