Overview

Namespaces

  • Webmozart
    • Console
      • Adapter
      • Api
        • Application
        • Args
          • Format
        • Command
        • Config
        • Event
        • Formatter
        • IO
        • Resolver
      • Args
      • Config
      • Formatter
      • Handler
        • Help
      • IO
        • InputStream
        • OutputStream
      • Process
      • Resolver
      • UI
        • Alignment
        • Component
        • Help
        • Layout
        • Style
      • Util

Classes

  • Webmozart\Console\Adapter\ApplicationAdapter
  • Webmozart\Console\Adapter\ArgsFormatInputDefinition
  • Webmozart\Console\Adapter\ArgsInput
  • Webmozart\Console\Adapter\CommandAdapter
  • Webmozart\Console\Adapter\FormatterAdapter
  • Webmozart\Console\Adapter\IOOutput
  • Webmozart\Console\Adapter\StyleConverter
  • Webmozart\Console\Api\Args\Args
  • Webmozart\Console\Api\Args\Format\AbstractOption
  • Webmozart\Console\Api\Args\Format\ArgsFormat
  • Webmozart\Console\Api\Args\Format\ArgsFormatBuilder
  • Webmozart\Console\Api\Args\Format\Argument
  • Webmozart\Console\Api\Args\Format\CommandName
  • Webmozart\Console\Api\Args\Format\CommandOption
  • Webmozart\Console\Api\Args\Format\Option
  • Webmozart\Console\Api\Command\Command
  • Webmozart\Console\Api\Command\CommandCollection
  • Webmozart\Console\Api\Config\ApplicationConfig
  • Webmozart\Console\Api\Config\CommandConfig
  • Webmozart\Console\Api\Config\Config
  • Webmozart\Console\Api\Config\OptionCommandConfig
  • Webmozart\Console\Api\Config\SubCommandConfig
  • Webmozart\Console\Api\Event\ConfigEvent
  • Webmozart\Console\Api\Event\ConsoleEvents
  • Webmozart\Console\Api\Event\PreHandleEvent
  • Webmozart\Console\Api\Event\PreResolveEvent
  • Webmozart\Console\Api\Formatter\Style
  • Webmozart\Console\Api\Formatter\StyleSet
  • Webmozart\Console\Api\IO\Input
  • Webmozart\Console\Api\IO\IO
  • Webmozart\Console\Api\IO\Output
  • Webmozart\Console\Api\Resolver\ResolvedCommand
  • Webmozart\Console\Args\ArgvArgs
  • Webmozart\Console\Args\DefaultArgsParser
  • Webmozart\Console\Args\StringArgs
  • Webmozart\Console\Args\TokenParser
  • Webmozart\Console\Config\DefaultApplicationConfig
  • Webmozart\Console\ConsoleApplication
  • Webmozart\Console\Formatter\AnsiFormatter
  • Webmozart\Console\Formatter\DefaultStyleSet
  • Webmozart\Console\Formatter\NullFormatter
  • Webmozart\Console\Formatter\PlainFormatter
  • Webmozart\Console\Handler\CallbackHandler
  • Webmozart\Console\Handler\DelegatingHandler
  • Webmozart\Console\Handler\Help\HelpAsciiDocHandler
  • Webmozart\Console\Handler\Help\HelpHandler
  • Webmozart\Console\Handler\Help\HelpJsonHandler
  • Webmozart\Console\Handler\Help\HelpManHandler
  • Webmozart\Console\Handler\Help\HelpTextHandler
  • Webmozart\Console\Handler\Help\HelpXmlHandler
  • Webmozart\Console\Handler\NullHandler
  • Webmozart\Console\IO\BufferedIO
  • Webmozart\Console\IO\ConsoleIO
  • Webmozart\Console\IO\InputStream\NullInputStream
  • Webmozart\Console\IO\InputStream\StandardInputStream
  • Webmozart\Console\IO\InputStream\StreamInputStream
  • Webmozart\Console\IO\InputStream\StringInputStream
  • Webmozart\Console\IO\OutputStream\BufferedOutputStream
  • Webmozart\Console\IO\OutputStream\ErrorOutputStream
  • Webmozart\Console\IO\OutputStream\NullOutputStream
  • Webmozart\Console\IO\OutputStream\StandardOutputStream
  • Webmozart\Console\IO\OutputStream\StreamOutputStream
  • Webmozart\Console\Process\ProcessLauncher
  • Webmozart\Console\Resolver\DefaultResolver
  • Webmozart\Console\Resolver\ResolveResult
  • Webmozart\Console\UI\Alignment\LabelAlignment
  • Webmozart\Console\UI\Component\BorderUtil
  • Webmozart\Console\UI\Component\CellWrapper
  • Webmozart\Console\UI\Component\EmptyLine
  • Webmozart\Console\UI\Component\ExceptionTrace
  • Webmozart\Console\UI\Component\Grid
  • Webmozart\Console\UI\Component\LabeledParagraph
  • Webmozart\Console\UI\Component\NameVersion
  • Webmozart\Console\UI\Component\Paragraph
  • Webmozart\Console\UI\Component\Table
  • Webmozart\Console\UI\Help\AbstractHelp
  • Webmozart\Console\UI\Help\ApplicationHelp
  • Webmozart\Console\UI\Help\CommandHelp
  • Webmozart\Console\UI\Layout\BlockLayout
  • Webmozart\Console\UI\Rectangle
  • Webmozart\Console\UI\Style\Alignment
  • Webmozart\Console\UI\Style\BorderStyle
  • Webmozart\Console\UI\Style\GridStyle
  • Webmozart\Console\UI\Style\TableStyle
  • Webmozart\Console\Util\ProcessTitle
  • Webmozart\Console\Util\SimilarCommandName
  • Webmozart\Console\Util\StringUtil

Interfaces

  • Webmozart\Console\Api\Application\Application
  • Webmozart\Console\Api\Args\ArgsParser
  • Webmozart\Console\Api\Args\RawArgs
  • Webmozart\Console\Api\Formatter\Formatter
  • Webmozart\Console\Api\IO\InputStream
  • Webmozart\Console\Api\IO\OutputStream
  • Webmozart\Console\Api\Resolver\CommandResolver
  • Webmozart\Console\UI\Component

Exceptions

  • Webmozart\Console\Api\Args\CannotAddArgumentException
  • Webmozart\Console\Api\Args\CannotAddOptionException
  • Webmozart\Console\Api\Args\CannotParseArgsException
  • Webmozart\Console\Api\Args\Format\InvalidValueException
  • Webmozart\Console\Api\Args\NoSuchArgumentException
  • Webmozart\Console\Api\Args\NoSuchOptionException
  • Webmozart\Console\Api\Command\CannotAddCommandException
  • Webmozart\Console\Api\Command\NoSuchCommandException
  • Webmozart\Console\Api\IO\IOException
  • Webmozart\Console\Api\Resolver\CannotResolveCommandException
  • Overview
  • Namespace
  • Class
  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: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 895: 896: 897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911: 912: 913: 
<?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 Webmozart\Assert\Assert;
use Webmozart\Console\Api\Args\CannotAddArgumentException;
use Webmozart\Console\Api\Args\CannotAddOptionException;
use Webmozart\Console\Api\Args\NoSuchArgumentException;
use Webmozart\Console\Api\Args\NoSuchOptionException;

/**
 * A builder for {@link ArgsFormat} instances.
 *
 * Use the methods in this class to dynamically build {@link ArgsFormat}
 * instances. When you are done configuring the builder, call
 * {@link getFormat()} to build an immutable {@link ArgsFormat}.
 *
 * For convenience, you can call {@link ArgsFormat::build()} to create a new
 * builder and use its fluent API to configure and build a format:
 *
 * ```php
 * $format = ArgsFormat::build()
 *     ->addCommandName(new CommandName('server'))
 *     ->addCommandOption(new CommandOption('add', 'a'))
 *     ->addArgument(new Argument('host'))
 *     ->addOption(new Option('port', 'p'))
 *     ->getFormat();
 * ```
 *
 * You can optionally pass a base format to inherit from. The arguments of the
 * base format are prepended to the arguments of the built format. The options
 * of the base format are added to the built options:
 *
 * ```php
 * $baseFormat = ArgsFormat::build()
 *     ->addOption(new Option('verbose', 'v'))
 *     ->getFormat();
 *
 * $format = ArgsFormat::build($baseFormat)
 *     // ...
 *     ->getFormat();
 * ```
 *
 * Read {@link ArgsFormat} for a more detailed description of args formats.
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @see    ArgsFormat
 */
class ArgsFormatBuilder
{
    /**
     * @var ArgsFormat
     */
    private $baseFormat;

    /**
     * @var CommandName[]
     */
    private $commandNames = array();

    /**
     * @var Option[]
     */
    private $commandOptions = array();

    /**
     * @var Option[]
     */
    private $commandOptionsByShortName = array();

    /**
     * @var Argument[]
     */
    private $arguments = array();

    /**
     * @var Option[]
     */
    private $options = array();

    /**
     * @var Option[]
     */
    private $optionsByShortName = array();

    /**
     * @var bool
     */
    private $hasMultiValuedArg = false;

    /**
     * @var bool
     */
    private $hasOptionalArg = false;

    /**
     * Creates a new builder.
     *
     * You can optionally pass a base format. The built format inherits all the
     * arguments and options from the base format.
     *
     * @param ArgsFormat $baseFormat The base format.
     */
    public function __construct(ArgsFormat $baseFormat = null)
    {
        $this->baseFormat = $baseFormat;
    }

    /**
     * Returns the base format.
     *
     * @return ArgsFormat The base format.
     */
    public function getBaseFormat()
    {
        return $this->baseFormat;
    }

    /**
     * Sets the command names of the built format.
     *
     * @param CommandName[] $commandNames The command names.
     *
     * @return static The current instance.
     */
    public function setCommandNames(array $commandNames)
    {
        $this->commandNames = array();

        $this->addCommandNames($commandNames);

        return $this;
    }

    /**
     * Adds command names to the built format.
     *
     * @param CommandName[] $commandNames The command names to add.
     *
     * @return static The current instance.
     */
    public function addCommandNames(array $commandNames)
    {
        foreach ($commandNames as $commandName) {
            $this->addCommandName($commandName);
        }

        return $this;
    }

    /**
     * Adds a command name to the built format.
     *
     * @param CommandName $commandName The command name to add.
     *
     * @return static The current instance.
     */
    public function addCommandName(CommandName $commandName)
    {
        $this->commandNames[] = $commandName;

        return $this;
    }

    /**
     * Returns whether the builder contains any command names.
     *
     * @param bool $includeBase Whether to consider command names of the base
     *                          format.
     *
     * @return bool Returns `true` if the builder contains any 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 all command names added to the builder.
     *
     * @param bool $includeBase Whether to include command names of 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;
    }

    /**
     * Sets the command options of the built format.
     *
     * Any existing command options are removed when this method is called.
     *
     * @param CommandOption[] $commandOptions The command options of the built
     *                                        format.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If an option cannot be added.
     *
     * @see addCommandOption()
     */
    public function setCommandOptions(array $commandOptions)
    {
        $this->commandOptions = array();
        $this->commandOptionsByShortName = array();

        $this->addCommandOptions($commandOptions);

        return $this;
    }

    /**
     * Adds command options to the builder.
     *
     * The existing command options stored in the builder are preserved.
     *
     * @param CommandOption[] $commandOptions The command options to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If an option cannot be added.
     *
     * @see addCommandOption()
     */
    public function addCommandOptions(array $commandOptions)
    {
        foreach ($commandOptions as $commandOption) {
            $this->addCommandOption($commandOption);
        }

        return $this;
    }

    /**
     * Adds a command option to the builder.
     *
     * The existing command options stored in the builder are preserved.
     *
     * @param CommandOption $commandOption The command option to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If the option cannot be added.
     *
     * @see addCommandOptions()
     */
    public function addCommandOption(CommandOption $commandOption)
    {
        $longName = $commandOption->getLongName();
        $shortName = $commandOption->getShortName();
        $longAliases = $commandOption->getLongAliases();
        $shortAliases = $commandOption->getShortAliases();

        if ($this->hasOption($longName) || $this->hasCommandOption($longName)) {
            throw CannotAddOptionException::existsAlready($longName);
        }

        foreach ($longAliases as $shortAlias) {
            if ($this->hasOption($shortAlias) || $this->hasCommandOption($shortAlias)) {
                throw CannotAddOptionException::existsAlready($shortAlias);
            }
        }

        if ($shortName && ($this->hasOption($shortName) || $this->hasCommandOption($shortName))) {
            throw CannotAddOptionException::existsAlready($shortName);
        }

        foreach ($shortAliases as $shortAlias) {
            if ($this->hasOption($shortAlias) || $this->hasCommandOption($shortAlias)) {
                throw CannotAddOptionException::existsAlready($shortAlias);
            }
        }

        $this->commandOptions[$longName] = $commandOption;

        if ($shortName) {
            $this->commandOptionsByShortName[$shortName] = $commandOption;
        }

        foreach ($longAliases as $longAlias) {
            $this->commandOptions[$longAlias] = $commandOption;
        }

        foreach ($shortAliases as $shortAlias) {
            $this->commandOptionsByShortName[$shortAlias] = $commandOption;
        }

        return $this;
    }

    /**
     * Returns whether the builder 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 command 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 builder contains any command options.
     *
     * @param bool $includeBase Whether to include command  options in the base
     *                          format in the search.
     *
     * @return bool Returns `true` if the builder 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 a command option by its long or short name.
     *
     * @param string $name        The long or short option name.
     * @param bool   $includeBase Whether to include command 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. 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 added to the builder.
     *
     * @param bool $includeBase Whether to include command 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;
    }

    /**
     * Sets the arguments of the built format.
     *
     * Any existing arguments are removed when this method is called.
     *
     * @param Argument[] $arguments The arguments of the built format.
     *
     * @return static The current instance.
     *
     * @throws CannotAddArgumentException If an argument cannot be added.
     *
     * @see addArgument()
     */
    public function setArguments(array $arguments)
    {
        $this->arguments = array();
        $this->hasOptionalArg = false;
        $this->hasMultiValuedArg = false;

        $this->addArguments($arguments);

        return $this;
    }

    /**
     * Adds arguments at the end of the argument list.
     *
     * The existing arguments stored in the builder are preserved.
     *
     * @param Argument[] $arguments The arguments to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddArgumentException If an argument cannot be added.
     *
     * @see addArgument()
     */
    public function addArguments(array $arguments)
    {
        foreach ($arguments as $argument) {
            $this->addArgument($argument);
        }

        return $this;
    }

    /**
     * Adds an argument at the end of the argument list.
     *
     * The existing arguments stored in the builder are preserved.
     *
     * You cannot add arguments after adding a multi-valued argument. If you do
     * so, this method throws an exception.
     *
     * Adding required arguments after optional arguments is not supported.
     * Also in this case an exception is thrown.
     *
     * @param Argument $argument The argument to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddArgumentException If the argument cannot be added.
     */
    public function addArgument(Argument $argument)
    {
        $name = $argument->getName();

        if ($this->hasArgument($name)) {
            throw CannotAddArgumentException::existsAlready($name);
        }

        if ($this->hasMultiValuedArgument()) {
            throw CannotAddArgumentException::cannotAddAfterMultiValued();
        }

        if ($argument->isRequired() && $this->hasOptionalArgument()) {
            throw CannotAddArgumentException::cannotAddRequiredAfterOptional();
        }

        if ($argument->isMultiValued()) {
            $this->hasMultiValuedArg = true;
        }

        if ($argument->isOptional()) {
            $this->hasOptionalArg = true;
        }

        $this->arguments[$name] = $argument;

        return $this;
    }

    /**
     * Returns whether the builder 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 builder 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 builder 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 builder contains an optional argument.
     *
     * @param bool $includeBase Whether to include arguments in the base format
     *                          in the search.
     *
     * @return bool Returns `true` if the builder 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 builder contains a required argument.
     *
     * @param bool $includeBase Whether to include arguments in the base format
     *                          in the search.
     *
     * @return bool Returns `true` if the builder 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 builder contains any argument.
     *
     * @param bool $includeBase Whether to include arguments in the base format
     *                          in the search.
     *
     * @return bool Returns `true` if the builder 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 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 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 added to the builder.
     *
     * @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;
    }

    /**
     * Sets the options of the built format.
     *
     * Any existing options are removed when this method is called.
     *
     * @param Option[] $options The options of the built format.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If an option cannot be added.
     *
     * @see addOption()
     */
    public function setOptions(array $options)
    {
        $this->options = array();
        $this->optionsByShortName = array();

        $this->addOptions($options);

        return $this;
    }

    /**
     * Adds options at the end of the options list.
     *
     * The existing options stored in the builder are preserved.
     *
     * @param Option[] $options The options to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If an option cannot be added.
     *
     * @see addOption()
     */
    public function addOptions(array $options)
    {
        foreach ($options as $option) {
            $this->addOption($option);
        }

        return $this;
    }

    /**
     * Adds an option at the end of the options list.
     *
     * The existing options stored in the builder are preserved.
     *
     * @param Option $option The option to add.
     *
     * @return static The current instance.
     *
     * @throws CannotAddOptionException If the option cannot be added.
     *
     * @see addOptions()
     */
    public function addOption(Option $option)
    {
        $longName = $option->getLongName();
        $shortName = $option->getShortName();

        if ($this->hasOption($longName) || $this->hasCommandOption($longName)) {
            throw CannotAddOptionException::existsAlready($longName);
        }

        if ($shortName && ($this->hasOption($shortName) || $this->hasCommandOption($shortName))) {
            throw CannotAddOptionException::existsAlready($shortName);
        }

        $this->options[$longName] = $option;

        if ($shortName) {
            $this->optionsByShortName[$shortName] = $option;
        }

        return $this;
    }

    /**
     * Returns whether the builder 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 builder contains any option.
     *
     * @param bool $includeBase Whether to include options in the base format
     *                          in the search.
     *
     * @return bool Returns `true` if the builder contains any option 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;
    }

    /**
     * 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. 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 added to the builder.
     *
     * @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;
    }

    /**
     * Builds a format with the arguments and options added to the builder.
     *
     * @return ArgsFormat The built format.
     */
    public function getFormat()
    {
        return new ArgsFormat($this, $this->baseFormat);
    }
}
Webmozart Console API API documentation generated by ApiGen