Show default value in help

master 1.2.0
Julien Rosset 6 years ago
parent 382b554be6
commit 9298f862be

@ -10,10 +10,10 @@ use CommandLine\CommandLine;
$cmdline = new CommandLine('Test', 'Programme de test', 'php test.php'); $cmdline = new CommandLine('Test', 'Programme de test', 'php test.php');
$cmdline->addDefaultArguments(); $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('days', 'Nombre jour', new IntegerParser(0, 365)))->setDefault(3));
$cmdline->addOption((new Value('years', 'Nombre d\'années', new IntegerParser(0)))->setDefault(5)); $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')); $args = $cmdline->parseExplicit(array('--help'));
$cmdline->treatDefaultArguments($args, false); $cmdline->treatDefaultArguments($args, false);

19
composer.lock generated

@ -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": []
}

@ -688,6 +688,7 @@ class CommandLine {
$pads->name = 0; $pads->name = 0;
$pads->occurMin = 0; $pads->occurMin = 0;
$pads->occurMax = 0; $pads->occurMax = 0;
$pads->default = 0;
$pads->valueDescription = 0; $pads->valueDescription = 0;
// Lecture des arguments // Lecture des arguments
@ -698,17 +699,30 @@ class CommandLine {
$pads->occurMin = max($pads->occurMin, strlen($value->getOccurMin())); $pads->occurMin = max($pads->occurMin, strlen($value->getOccurMin()));
$pads->occurMax = max($pads->occurMax, strlen(is_null($max) ? 'N' : $max)); $pads->occurMax = max($pads->occurMax, strlen(is_null($max) ? 'N' : $max));
$pads->default = max($pads->default, strlen($value->getDefault()));
if ($value instanceof IArgumentValueDescription) { if ($value instanceof IArgumentValueDescription) {
$pads->valueDescription = max($pads->valueDescription, strlen($value->getValueDescription())); $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 * Génération des descriptifs
*/ */
$entries = array(); $entries = array();
foreach ($values as $value) { 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(); $max = $value->getOccurMax();
@ -716,35 +730,27 @@ class CommandLine {
$occur .= str_pad($value->getOccurMin(), $pads->occurMin, ' ', STR_PAD_LEFT); $occur .= str_pad($value->getOccurMin(), $pads->occurMin, ' ', STR_PAD_LEFT);
$occur .= ' => '; $occur .= ' => ';
$occur .= str_pad(is_null($max) ? 'N' : $max, $pads->occurMax, ' ', STR_PAD_RIGHT); $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( $entry[] = preg_replace(
$value instanceof IArgumentValueDescription ? $value->getValueDescription() : '', '@\r?\n@',
$pads->valueDescription, '$0' . self::TAB . implode(self::TAB, $spaces),
' ', $value->getDescription()
STR_PAD_RIGHT
); );
$entries[] = self::TAB . implode( $entries[] = self::TAB . implode(self::TAB, $entry);
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()
),
)
);
} }
return $entries; return $entries;
@ -764,6 +770,7 @@ class CommandLine {
$pads = new stdClass; $pads = new stdClass;
$pads->tagShort = 0; $pads->tagShort = 0;
$pads->tagLong = 0; $pads->tagLong = 0;
$pads->default = 0;
$pads->valueDescription = 0; $pads->valueDescription = 0;
// Lecture des arguments // Lecture des arguments
@ -774,6 +781,7 @@ class CommandLine {
} }
$pads->tagLong = max($pads->tagLong, strlen($option->getTagLong())); $pads->tagLong = max($pads->tagLong, strlen($option->getTagLong()));
$pads->default = max($pads->default, strlen($option->getDefault()));
if ($option instanceof IArgumentValueDescription) { if ($option instanceof IArgumentValueDescription) {
$pads->valueDescription = max($pads->valueDescription, strlen($option->getValueDescription())); $pads->valueDescription = max($pads->valueDescription, strlen($option->getValueDescription()));
} }
@ -783,11 +791,23 @@ class CommandLine {
$pads->tagShort += 1; $pads->tagShort += 1;
$pads->tagLong += 2; $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 * Génération des descriptifs
*/ */
$entries = array(); $entries = array();
foreach ($options as $option) { foreach ($options as $option) {
$entry = array();
$short = $option->getTagShort(); $short = $option->getTagShort();
$label = ''; $label = '';
@ -795,33 +815,28 @@ class CommandLine {
$label .= ' '; $label .= ' ';
$label .= str_pad('--' . $option->getTagLong(), $pads->tagLong, ' ', STR_PAD_RIGHT); $label .= str_pad('--' . $option->getTagLong(), $pads->tagLong, ' ', STR_PAD_RIGHT);
$label .= $option->allowMultiple() ? ' *' : ($option->isStoppingParse() ? ' X' : ' '); $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( $entry[] = preg_replace(
$option instanceof IArgumentValueDescription ? $option->getValueDescription() : '', '@\r?\n@',
$pads->valueDescription, '$0' . self::TAB . implode(self::TAB, $spaces),
' ', $option->getDescription()
STR_PAD_RIGHT
); );
$entries[] = self::TAB . implode( $entries[] = self::TAB . implode(self::TAB, $entry);
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()
),
)
);
} }
return $entries; return $entries;

Loading…
Cancel
Save