1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
<?php
namespace Webmozart\Console\Api\Args\Format;
use Webmozart\Assert\Assert;
class CommandOption extends AbstractOption
{
private $longAliases = array();
private $shortAliases = array();
public function __construct($longName, $shortName = null, array $aliases = array(), $flags = 0, $description = null)
{
parent::__construct($longName, $shortName, $flags, $description);
foreach ($aliases as $key => $alias) {
$alias = $this->removeDashPrefix($alias);
if (1 === strlen($alias)) {
$this->assertShortAliasValid($alias);
$this->shortAliases[] = $alias;
} else {
$this->assertLongAliasValid($alias);
$this->longAliases[] = $alias;
}
}
}
public function getLongAliases()
{
return $this->longAliases;
}
public function getShortAliases()
{
return $this->shortAliases;
}
private function removeDashPrefix($string)
{
if ('--' === substr($string, 0, 2)) {
$string = substr($string, 2);
} elseif (isset($string[0]) && '-' === $string[0]) {
$string = substr($string, 1);
}
return $string;
}
private function assertLongAliasValid($alias)
{
Assert::string($alias, 'An option alias must be a string or null. Got: %s');
Assert::notEmpty($alias, 'An option alias must not be empty.');
Assert::startsWithLetter($alias, 'A long option alias must start with a letter.');
Assert::regex($alias, '~^[a-zA-Z0-9\-]+$~', 'A long option alias must contain letters, digits and hyphens only.');
}
private function assertShortAliasValid($alias)
{
Assert::string($alias, 'An option alias must be a string or null. Got: %s');
Assert::notEmpty($alias, 'An option alias must not be empty.');
Assert::regex($alias, '~^[a-zA-Z]$~', 'A short option alias must be exactly one letter. Got: "%s"');
}
}