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: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749:
<?php
/*
* This file is part of the webmozart/console package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Console\Api\Args\Format;
use InvalidArgumentException;
use Webmozart\Assert\Assert;
use Webmozart\Console\Api\Args\NoSuchArgumentException;
use Webmozart\Console\Api\Args\NoSuchOptionException;
/**
* The format used to parse a {@link RawArgs} instance.
*
* This class is a container for {@link CommandName}, {@link CommandOption},
* {@link Option} and {@link Argument} objects. The format is used to interpret
* a given {@link RawArgs} instance.
*
* You can pass the options and arguments to the constructor of the class:
*
* ```php
* $format = new ArgsFormat(array(
* new CommandName('server'),
* new CommandName('add'),
* new Argument('host', Argument::REQUIRED),
* new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
* ));
* ```
*
* The previous example configures a command that can be called like this:
*
* ```
* $ console server add localhost
* $ console server add localhost --port 8080
* ```
*
* If the "add" command should be called via an option, change the format to:
*
* ```php
* $format = new ArgsFormat(array(
* new CommandName('server'),
* new CommandOption('add', 'a'),
* new Argument('host', Argument::REQUIRED),
* new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
* ));
* ```
*
* The command is then called like this:
*
* ```
* $ console server --add localhost
* $ console server --add localhost --port 8080
* ```
*
* The format is immutable after its construction. This is necessary to maintain
* consistency when one format inherits from another. For example, adding a
* required argument to the base format of a format that already contains
* optional arguments is an illegal operation that cannot be prevented if the
* formats are mutable.
*
* If you want to create a format stepwisely, use an {@link ArgsFormatBuilder}.
*
* If multiple formats share a common set of options and arguments, extract
* these options and arguments into a base format and let the other formats
* inherit from this base format:
*
* ```php
* $baseFormat = new ArgsFormat(array(
* new Option('verbose', 'v'),
* ));
*
* $format = new ArgsFormat(array(
* new CommandName('server'),
* new CommandName('add'),
* new Argument('host', Argument::REQUIRED),
* new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
* ), $baseFormat);
* ```
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ArgsFormat
{
/**
* @var ArgsFormat
*/
private $baseFormat;
/**
* @var CommandName[]
*/
private $commandNames;
/**
* @var CommandOption[]
*/
private $commandOptions = array();
/**
* @var CommandOption[]
*/
private $commandOptionsByShortName = array();
/**
* @var Argument[]
*/
private $arguments;
/**
* @var Option[]
*/
private $options;
/**
* @var Option[]
*/
private $optionsByShortName = array();
/**
* @var bool
*/
private $hasMultiValuedArg = false;
/**
* @var bool
*/
private $hasOptionalArg = false;
/**
* Returns a format builder.
*
* You can optionally pass a base format. The built format inherits all the
* arguments and options defined in the base format.
*
* @param ArgsFormat $baseFormat The base format.
*
* @return ArgsFormatBuilder The created builder.
*/
public static function build(ArgsFormat $baseFormat = null)
{
return new ArgsFormatBuilder($baseFormat);
}
/**
* Creates a new format.
*
* You can optionally pass a base format. The created format inherits all
* the arguments and options defined in the base format.
*
* @param array|ArgsFormatBuilder $elements The arguments and options or a
* builder instance.
* @param ArgsFormat $baseFormat The format.
*/
public function __construct($elements = array(), ArgsFormat $baseFormat = null)
{
if ($elements instanceof ArgsFormatBuilder) {
$builder = $elements;
} else {
$builder = $this->createBuilderForElements($elements, $baseFormat);
}
if (null === $baseFormat) {
$baseFormat = $builder->getBaseFormat();
}
$this->baseFormat = $baseFormat;
$this->commandNames = $builder->getCommandNames(false);
$this->arguments = $builder->getArguments(false);
$this->options = $builder->getOptions(false);
$this->hasMultiValuedArg = $builder->hasMultiValuedArgument(false);
$this->hasOptionalArg = $builder->hasOptionalArgument(false);
foreach ($this->options as $option) {
if ($option->getShortName()) {
$this->optionsByShortName[$option->getShortName()] = $option;
}
}
foreach ($builder->getCommandOptions(false) as $commandOption) {
$this->commandOptions[$commandOption->getLongName()] = $commandOption;
if ($commandOption->getShortName()) {
$this->commandOptionsByShortName[$commandOption->getShortName()] = $commandOption;
}
foreach ($commandOption->getLongAliases() as $longAlias) {
$this->commandOptions[$longAlias] = $commandOption;
}
foreach ($commandOption->getShortAliases() as $shortAlias) {
$this->commandOptionsByShortName[$shortAlias] = $commandOption;
}
}
}
/**
* Returns the base format.
*
* @return ArgsFormat The base format.
*/
public function getBaseFormat()
{
return $this->baseFormat;
}
/**
* Returns the command names.
*
* @param bool $includeBase Whether to include command names in the base
* format in the result.
*
* @return CommandName[] The command names.
*/
public function getCommandNames($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$commandNames = $this->commandNames;
if ($includeBase && $this->baseFormat) {
$commandNames = array_merge($this->baseFormat->getCommandNames(), $commandNames);
}
return $commandNames;
}
/**
* Returns whether the format contains any command names.
*
* @param bool $includeBase Whether to consider command names in the base
* format.
*
* @return bool Returns `true` if the format contains command names and
* `false` otherwise.
*/
public function hasCommandNames($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (count($this->commandNames) > 0) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasCommandNames();
}
return false;
}
/**
* Returns a command option by its long or short name.
*
* @param string $name The long or short option name.
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return CommandOption The command option.
*
* @throws NoSuchOptionException If the command option with the given name
* does not not exist.
*/
public function getCommandOption($name, $includeBase = true)
{
Assert::string($name, 'The option name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The option name must not be empty.');
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (isset($this->commandOptions[$name])) {
return $this->commandOptions[$name];
}
if (isset($this->commandOptionsByShortName[$name])) {
return $this->commandOptionsByShortName[$name];
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->getCommandOption($name);
}
throw NoSuchOptionException::forOptionName($name);
}
/**
* Returns all command options of the format.
*
* @param bool $includeBase Whether to include options of the base format
* in the result.
*
* @return CommandOption[] The command options.
*/
public function getCommandOptions($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$commandOptions = array_values($this->commandOptions);
if ($includeBase && $this->baseFormat) {
// prepend base command options
$commandOptions = array_merge($this->baseFormat->getCommandOptions(), $commandOptions);
}
return $commandOptions;
}
/**
* Returns whether the format contains a specific command option.
*
* You can either pass the long or the short name of the command option.
*
* @param string $name The long or short option name.
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return bool Returns `true` if the command option with the given name
* could be found and `false` otherwise.
*/
public function hasCommandOption($name, $includeBase = true)
{
Assert::string($name, 'The option name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The option name must not be empty.');
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (isset($this->commandOptions[$name]) || isset($this->commandOptionsByShortName[$name])) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasCommandOption($name);
}
return false;
}
/**
* Returns whether the format contains command options.
*
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return bool Returns `true` if the format contains command options and
* `false` otherwise.
*/
public function hasCommandOptions($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (count($this->commandOptions) > 0) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasCommandOptions();
}
return false;
}
/**
* Returns an argument by its name or position.
*
* You can either pass the name of the argument or the 0-based position of
* the argument.
*
* @param string|int $name The argument name or its 0-based position
* in the argument list.
* @param bool $includeBase Whether to include arguments in the base
* format in the search.
*
* @return Argument The argument.
*
* @throws NoSuchArgumentException If the argument with the given name or
* position does not exist.
*/
public function getArgument($name, $includeBase = true)
{
if (!is_int($name)) {
Assert::string($name, 'The argument name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The argument name must not be empty.');
}
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (is_int($name)) {
$arguments = array_values($this->getArguments($includeBase));
if (!isset($arguments[$name])) {
throw NoSuchArgumentException::forPosition($name);
}
} else {
$arguments = $this->getArguments($includeBase);
if (!isset($arguments[$name])) {
throw NoSuchArgumentException::forArgumentName($name);
}
}
return $arguments[$name];
}
/**
* Returns all arguments of the format.
*
* @param bool $includeBase Whether to include arguments of the base format
* in the result.
*
* @return Argument[] The arguments.
*/
public function getArguments($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$arguments = $this->arguments;
if ($includeBase && $this->baseFormat) {
// prepend base arguments
$arguments = array_replace($this->baseFormat->getArguments(), $arguments);
}
return $arguments;
}
/**
* Returns whether the format contains a specific argument.
*
* You can either pass the name of the argument or the 0-based position of
* the argument.
*
* @param string|int $name The argument name or its 0-based position
* in the argument list.
* @param bool $includeBase Whether to include arguments in the base
* format in the search.
*
* @return bool Returns `true` if the argument with the given name or
* position could be found and `false` otherwise.
*/
public function hasArgument($name, $includeBase = true)
{
if (!is_int($name)) {
Assert::string($name, 'The argument name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The argument name must not be empty.');
}
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$arguments = is_int($name)
? array_values($this->getArguments($includeBase))
: $this->getArguments($includeBase);
return isset($arguments[$name]);
}
/**
* Returns whether the format contains a multi-valued argument.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the search.
*
* @return bool Returns `true` if the format contains a multi-valued
* argument and `false` otherwise.
*/
public function hasMultiValuedArgument($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if ($this->hasMultiValuedArg) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasMultiValuedArgument();
}
return false;
}
/**
* Returns whether the format contains an optional argument.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the search.
*
* @return bool Returns `true` if the format contains an optional argument
* and `false` otherwise.
*/
public function hasOptionalArgument($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if ($this->hasOptionalArg) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasOptionalArgument();
}
return false;
}
/**
* Returns whether the format contains a required argument.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the search.
*
* @return bool Returns `true` if the format contains a required argument
* and `false` otherwise.
*/
public function hasRequiredArgument($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (!$this->hasOptionalArg && count($this->arguments) > 0) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasRequiredArgument();
}
return false;
}
/**
* Returns whether the format contains any argument.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the search.
*
* @return bool Returns `true` if the format contains any argument and
* `false` otherwise.
*/
public function hasArguments($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (count($this->arguments) > 0) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasArguments();
}
return false;
}
/**
* Returns the number of arguments.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the result.
*
* @return int The number of arguments.
*/
public function getNumberOfArguments($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if ($this->hasMultiValuedArg) {
return PHP_INT_MAX;
}
return count($this->getArguments($includeBase));
}
/**
* Returns the number of required arguments.
*
* @param bool $includeBase Whether to include arguments in the base format
* in the result.
*
* @return int The number of required arguments.
*/
public function getNumberOfRequiredArguments($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$arguments = $this->getArguments($includeBase);
$count = 0;
foreach ($arguments as $argument) {
if (!$argument->isRequired()) {
continue;
}
if ($argument->isMultiValued()) {
return PHP_INT_MAX;
}
++$count;
}
return $count;
}
/**
* Returns an option by its long or short name.
*
* @param string $name The long or short option name.
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return Option The option.
*
* @throws NoSuchOptionException If the option with the given name does not
* not exist.
*/
public function getOption($name, $includeBase = true)
{
Assert::string($name, 'The option name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The option name must not be empty.');
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (isset($this->options[$name])) {
return $this->options[$name];
}
if (isset($this->optionsByShortName[$name])) {
return $this->optionsByShortName[$name];
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->getOption($name);
}
throw NoSuchOptionException::forOptionName($name);
}
/**
* Returns all options of the format.
*
* @param bool $includeBase Whether to include options of the base format
* in the result.
*
* @return Option[] The options.
*/
public function getOptions($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
$options = $this->options;
if ($includeBase && $this->baseFormat) {
// append base options
$options = array_replace($options, $this->baseFormat->getOptions());
}
return $options;
}
/**
* Returns whether the format contains a specific option.
*
* You can either pass the long or the short name of the option.
*
* @param string $name The long or short option name.
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return bool Returns `true` if the option with the given name could be
* found and `false` otherwise.
*/
public function hasOption($name, $includeBase = true)
{
Assert::string($name, 'The option name must be a string or an integer. Got: %s');
Assert::notEmpty($name, 'The option name must not be empty.');
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (isset($this->options[$name]) || isset($this->optionsByShortName[$name])) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasOption($name);
}
return false;
}
/**
* Returns whether the format contains options.
*
* @param bool $includeBase Whether to include options in the base format
* in the search.
*
* @return bool Returns `true` if the format contains options and `false`
* otherwise.
*/
public function hasOptions($includeBase = true)
{
Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
if (count($this->options) > 0) {
return true;
}
if ($includeBase && $this->baseFormat) {
return $this->baseFormat->hasOptions();
}
return false;
}
/**
* Creates a format builder for a set of arguments and options.
*
* @param array $elements The arguments and options to add to the
* builder.
* @param ArgsFormat $baseFormat The base format.
*
* @return ArgsFormatBuilder The created builder.
*/
private function createBuilderForElements(array $elements, ArgsFormat $baseFormat = null)
{
$builder = new ArgsFormatBuilder($baseFormat);
foreach ($elements as $element) {
if ($element instanceof CommandName) {
$builder->addCommandName($element);
} elseif ($element instanceof CommandOption) {
$builder->addCommandOption($element);
} elseif ($element instanceof Option) {
$builder->addOption($element);
} elseif ($element instanceof Argument) {
$builder->addArgument($element);
} else {
throw new InvalidArgumentException(sprintf(
'Expected instances of CommandName, CommandOption, '.
'Option or Argument. Got: %s',
is_object($element) ? get_class($element) : gettype($element)
));
}
}
return $builder;
}
}