| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Parsoid\Tools; |
| 4 |
|
| 5 |
trait ExtendedOptsProcessor { |
| 6 |
private $optionDefaults = []; |
| 7 |
|
| 8 |
/** |
| 9 |
* @param string $name |
| 10 |
* @param mixed $default |
| 11 |
*/ |
| 12 |
public function setOptionDefault( string $name, $default ) { |
| 13 |
$this->optionDefaults[$name] = $default; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Add a parameter to the script with a default value. |
| 18 |
* Will be displayed on --help with the associated description. |
| 19 |
* |
| 20 |
* @param string $name The name of the param (help, version, etc) |
| 21 |
* @param string $description The description of the param to show on --help |
| 22 |
* @param mixed $defaultValue Default value (default null) |
| 23 |
* @param string|bool $shortName Character to use as short name |
| 24 |
* @param bool $required Is the param required? |
| 25 |
*/ |
| 26 |
public function addOptionWithDefault( |
| 27 |
string $name, string $description, |
| 28 |
$defaultValue = null, |
| 29 |
$shortName = false, |
| 30 |
bool $required = false |
| 31 |
) { |
| 32 |
$this->addOption( |
| 33 |
$name, |
| 34 |
"$description (default: $defaultValue)", |
| 35 |
$required, true, /* withArg */ |
| 36 |
$shortName, false /* multiOccurence */ |
| 37 |
); |
| 38 |
$this->setOptionDefault( |
| 39 |
$name, $defaultValue |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** @inheritDoc */ |
| 44 |
public function getOption( $name, $default = null ) { |
| 45 |
return parent::getOption( |
| 46 |
$name, $default ?? $this->optionDefaults[$name] ?? null |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Return all known CLI options in an associative array |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function optionsToArray(): array { |
| 55 |
return $this->getOptions() + $this->optionDefaults; |
| 56 |
} |
| 57 |
} |