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:  914:  915:  916:  917:  918:  919:  920:  921:  922:  923:  924:  925:  926:  927:  928:  929:  930:  931:  932:  933:  934:  935:  936:  937:  938:  939:  940:  941:  942:  943:  944:  945:  946:  947:  948:  949:  950:  951:  952:  953:  954:  955:  956:  957:  958:  959:  960:  961:  962:  963:  964:  965:  966:  967:  968:  969:  970:  971:  972:  973:  974:  975:  976:  977:  978:  979:  980:  981:  982:  983:  984:  985:  986:  987:  988:  989:  990:  991:  992:  993:  994:  995:  996:  997:  998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 
<?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\Config;

use Webmozart\Assert\Assert;
use Webmozart\Console\Api\Args\Format\ArgsFormat;
use Webmozart\Console\Api\Args\Format\CommandName;
use Webmozart\Console\Api\Command\NoSuchCommandException;

/**
 * The configuration of a console command.
 *
 * There are two different ways of creating a command configuration:
 *
 *  * Call {@link create()} or {@link ApplicationConfig::beginCommand()} and use
 *    the fluent interface:
 *
 *    ```php
 *    $config = CommandConfig::create()
 *        ->setName('server')
 *        ->setDescription('List and manage servers')
 *
 *        ->beginSubCommand('add')
 *            ->setDescription('Add a new server')
 *            ->addArgument('host', Argument::REQUIRED)
 *            ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80)
 *        ->end()
 *
 *        // ...
 *    ;
 *    ```
 *
 *  * Extend the class and implement the {@link configure()} method:
 *
 *    ```php
 *    class ServerCommandConfig extends CommandConfig
 *    {
 *        protected function configure()
 *        {
 *            $this
 *                ->setName('server')
 *                ->setDescription('List and manage servers')
 *
 *                ->beginSubCommand('add')
 *                    ->setDescription('Add a new server')
 *                    ->addArgument('host', Argument::REQUIRED)
 *                    ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80)
 *                ->end()
 *
 *                // ...
 *            ;
 *        }
 *    }
 *    ```
 *
 * You can choose between two different ways of executing a command:
 *
 *  * You can register a callback with {@link setCallback()}. The callback
 *    receives the input, the standard output and the error output as
 *    arguments:
 *
 *    ```php
 *    $config->setCallback(
 *        function (InputInterface $input, OutputInterface $output, OutputInterface $errorOutput) {
 *            // ...
 *        }
 *    );
 *    ```
 *
 *  * You can implement a custom command handler and return the handler from
 *    {@link getHandler()}. Since the command handler is separated, it can be
 *    easily tested:
 *
 *    ```php
 *    class ServerConfig extends CommandConfig
 *    {
 *        public function getHandler()
 *        {
 *            return new ServerHandler();
 *        }
 *    }
 *    ```
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class CommandConfig extends Config
{
    /**
     * @var string
     */
    private $name;

    /**
     * @var ApplicationConfig
     */
    private $applicationConfig;

    /**
     * @var string[]
     */
    private $aliases = array();

    /**
     * @var string
     */
    private $description;

    /**
     * @var string
     */
    private $help;

    /**
     * @var bool
     */
    private $enabled = true;

    /**
     * @var string
     */
    private $processTitle;

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

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

    /**
     * @var SubCommandConfig[]
     */
    private $subCommandConfigs = array();

    /**
     * Creates a new configuration.
     *
     * @param string            $name              The name of the command.
     * @param ApplicationConfig $applicationConfig The application configuration.
     *
     * @return static The created configuration.
     */
    public static function create($name = null, ApplicationConfig $applicationConfig = null)
    {
        return new static($name, $applicationConfig);
    }

    /**
     * Creates a new configuration.
     *
     * @param string            $name              The name of the command.
     * @param ApplicationConfig $applicationConfig The application configuration.
     */
    public function __construct($name = null, ApplicationConfig $applicationConfig = null)
    {
        $this->applicationConfig = $applicationConfig;

        parent::__construct();

        if ($name) {
            $this->setName($name);
        }
    }

    /**
     * Returns the name of the command.
     *
     * @return string The name of the command.
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Sets the name of the command.
     *
     * @param string $name The name of the command.
     *
     * @return static The current instance.
     */
    public function setName($name)
    {
        if (null !== $name) {
            Assert::string($name, 'The command name must be a string or null. Got: %s');
            Assert::notEmpty($name, 'The command name must not be empty.');
            Assert::regex($name, '~^[a-zA-Z0-9\-]+$~', 'The command name should contain letters, digits and hyphens only. Got: %s');
        }

        $this->name = $name;

        return $this;
    }

    /**
     * Returns the application configuration.
     *
     * @return ApplicationConfig The application configuration.
     */
    public function getApplicationConfig()
    {
        return $this->applicationConfig;
    }

    /**
     * Sets the application configuration.
     *
     * @param ApplicationConfig $applicationConfig The application configuration.
     */
    public function setApplicationConfig($applicationConfig)
    {
        $this->applicationConfig = $applicationConfig;
    }

    /**
     * Ends the block when dynamically configuring a command configuration.
     *
     * This method is usually used together with
     * {@link ApplicationConfig::beginCommand()}:
     *
     * ```php
     * $config
     *     ->beginCommand('command')
     *         // ...
     *     ->end()
     *
     *     // ...
     * ;
     * ```
     *
     * @return ApplicationConfig The application configuration.
     */
    public function end()
    {
        return $this->applicationConfig;
    }

    /**
     * Returns the alias names of the command.
     *
     * @return string[] An array of alias names of the command.
     *
     * @see addAlias(), setAliases()
     */
    public function getAliases()
    {
        return $this->aliases;
    }

    /**
     * Adds an alias name.
     *
     * An alias is an alternative name that can be used when calling the
     * command. Aliases are a useful way for migrating a command from one name
     * to another.
     *
     * Existing alias names are preserved.
     *
     * @param string $alias The alias name to add.
     *
     * @return static The current instance.
     *
     * @see addAliases(), setAliases(), getAlias()
     */
    public function addAlias($alias)
    {
        Assert::string($alias, 'The command alias must be a string. Got: %s');
        Assert::notEmpty($alias, 'The command alias must not be empty.');
        Assert::regex($alias, '~^[a-zA-Z0-9\-]+$~', 'The command alias should contain letters, digits and hyphens only. Got: %s');

        $this->aliases[] = $alias;

        return $this;
    }

    /**
     * Adds a list of alias names.
     *
     * Existing alias names are preserved.
     *
     * @param array $aliases The alias names to add.
     *
     * @return static The current instance.
     *
     * @see addAlias(), setAliases(), getAlias()
     */
    public function addAliases(array $aliases)
    {
        foreach ($aliases as $alias) {
            $this->addAlias($alias);
        }

        return $this;
    }

    /**
     * Sets the alias names of the command.
     *
     * Existing alias names are replaced.
     *
     * @param array $aliases The alias names.
     *
     * @return static The current instance.
     *
     * @see addAlias(), addAliases(), getAlias()
     */
    public function setAliases(array $aliases)
    {
        $this->aliases = array();

        $this->addAliases($aliases);

        return $this;
    }

    /**
     * Returns the description of the command.
     *
     * @return string The description of the command.
     *
     * @see setDescription()
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Sets the description of the command.
     *
     * The description is a short one-liner that describes the command in the
     * command listing. The description should be written in imperative form
     * rather than in descriptive form. So:
     *
     * > List the contents of a directory.
     *
     * should be preferred over
     *
     * > Lists the contents of a directory.
     *
     * @param string $description The description.
     *
     * @return static The current instance.
     *
     * @see getDescription()
     */
    public function setDescription($description)
    {
        if (null !== $description) {
            Assert::string($description, 'The command description must be a string or null. Got: %s');
            Assert::notEmpty($description, 'The command description must not be empty.');
        }

        $this->description = $description;

        return $this;
    }

    /**
     * Returns the help text of the command.
     *
     * The help text provides additional information about a command that is
     * displayed in the help view.
     *
     * @return string The help text of the command.
     *
     * @see setHelp()
     */
    public function getHelp()
    {
        return $this->help;
    }

    /**
     * Sets the help text of the command.
     *
     * The help text provides additional information about a command that is
     * displayed in the help view.
     *
     * @param string $help The help text of the command.
     *
     * @return static The current instance.
     *
     * @see getHelp()
     */
    public function setHelp($help)
    {
        if (null !== $help) {
            Assert::string($help, 'The help text must be a string or null. Got: %s');
            Assert::notEmpty($help, 'The help text must not be empty.');
        }

        $this->help = $help;

        return $this;
    }

    /**
     * Returns whether the command is enabled or not in the current environment.
     *
     * @return bool Returns `true` if the command is currently enabled and
     *              `false` otherwise.
     *
     * @see enable(), disable(), enableIf(), disableIf()
     */
    public function isEnabled()
    {
        return $this->enabled;
    }

    /**
     * Enables the command.
     *
     * @return static The current instance.
     *
     * @see enableIf(), disable(), isEnabled()
     */
    public function enable()
    {
        $this->enabled = true;

        return $this;
    }

    /**
     * Enables the command if a condition holds and disables it otherwise.
     *
     * @param bool $condition The condition under which to enable the command.
     *
     * @return static The current instance.
     *
     * @see enable(), disable(), isEnabled()
     */
    public function enableIf($condition)
    {
        $this->enabled = (bool) $condition;

        return $this;
    }

    /**
     * Disables the command.
     *
     * @return static The current instance.
     *
     * @see disableIf(), enable(), isEnabled()
     */
    public function disable()
    {
        $this->enabled = false;

        return $this;
    }

    /**
     * Disables the command if a condition holds and enables it otherwise.
     *
     * @param bool $condition The condition under which to disable the command.
     *
     * @return static The current instance.
     *
     * @see disable(), enable(), isEnabled()
     */
    public function disableIf($condition)
    {
        $this->enabled = !$condition;

        return $this;
    }

    /**
     * Returns the title of the command process.
     *
     * @return string|null The process title or `null` if no title should be
     *                     set.
     *
     * @see setProcessTitle()
     */
    public function getProcessTitle()
    {
        return $this->processTitle;
    }

    /**
     * Sets the title of the command process.
     *
     * @param string|null $processTitle The process title or `null` if no title
     *                                  should be set.
     *
     * @return static The current instance.
     *
     * @see getProcessTitle()
     */
    public function setProcessTitle($processTitle)
    {
        if (null !== $processTitle) {
            Assert::string($processTitle, 'The command process title must be a string or null. Got: %s');
            Assert::notEmpty($processTitle, 'The command process title must not be empty.');
        }

        $this->processTitle = $processTitle;

        return $this;
    }

    /**
     * Marks the command as default command.
     *
     * The names of default commands can be omitted when calling the command.
     * For example, the following command can be called in two ways:
     *
     * ```php
     * protected function configure()
     * {
     *     $this
     *         ->beginCommand('add')
     *             ->markDefault()
     *             ->addArgument('host', Argument::REQUIRED)
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * The first way is to call the command regularly. The second way is to
     * omit the name of the command:
     *
     * ```php
     * $ ./console add localhost
     * $ ./console localhost
     * ```
     *
     * @return static The current instance.
     *
     * @see markAnonymous(), markNoDefault()
     */
    public function markDefault()
    {
        $this->default = true;
        $this->anonymous = false;

        return $this;
    }

    /**
     * Marks the command as anonymous command.
     *
     * Anonymous commands cannot be called by name:
     *
     * ```php
     * protected function configure()
     * {
     *     $this
     *         ->beginCommand('add')
     *             ->markAnonymous()
     *             ->addArgument('host', Argument::REQUIRED)
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * The name "add" is given to the command only to access the command later
     * on. Since the command is anonymous, the name cannot be passed when
     * when calling the command:
     *
     * ```php
     * $ ./console add localhost
     * ```
     *
     * Instead, the command should be called without name:
     *
     * ```php
     * $ ./console localhost
     * ```
     *
     * @return static The current instance.
     *
     * @see markDefault(), markNoDefault()
     */
    public function markAnonymous()
    {
        $this->default = true;
        $this->anonymous = true;

        return $this;
    }

    /**
     * Marks the command as neither anonymous nor default.
     *
     * @return static The current instance.
     *
     * @see markDefault(), markAnonymous()
     */
    public function markNoDefault()
    {
        $this->default = false;
        $this->anonymous = false;

        return $this;
    }

    /**
     * Returns whether the command is a default command.
     *
     * @return bool Returns `true` if either {@link markDefault()} or
     *              {@link markAnonymous()} was called and `false` otherwise.
     */
    public function isDefault()
    {
        return $this->default;
    }

    /**
     * Returns whether the command is anonymous.
     *
     * @return bool Returns `true` if {@link markAnonymous()} was called and
     *              `false` otherwise.
     */
    public function isAnonymous()
    {
        return $this->anonymous;
    }

    /**
     * Builds an {@link ArgsFormat} instance with the given base format.
     *
     * @param ArgsFormat $baseFormat The base format.
     *
     * @return ArgsFormat The built format for the console arguments.
     */
    public function buildArgsFormat(ArgsFormat $baseFormat = null)
    {
        $formatBuilder = ArgsFormat::build($baseFormat);

        if (!$this->anonymous) {
            $formatBuilder->addCommandName(new CommandName($this->name, $this->aliases));
        }

        $formatBuilder->addOptions($this->getOptions());
        $formatBuilder->addArguments($this->getArguments());

        return $formatBuilder->getFormat();
    }

    /**
     * Starts a configuration block for a sub-command.
     *
     * A sub-command is executed if the name of the command is passed after the
     * name of the containing command. For example, if the command "server" has
     * a sub-command command named "add", that command can be called with:
     *
     * ```
     * $ console server add ...
     * ```
     *
     * The configuration of the sub-command is returned by this method. You can
     * use the fluent interface to configure the sub-command before jumping back
     * to this configuration with {@link SubCommandConfig::end()}:
     *
     * ```php
     * protected function configure()
     * {
     *     $this
     *         ->beginCommand('server')
     *             ->setDescription('List and manage servers')
     *
     *             ->beginSubCommand('add')
     *                 ->setDescription('Add a server')
     *                 ->addArgument('host', Argument::REQUIRED)
     *                 ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80)
     *             ->end()
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * @param string $name The name of the sub-command.
     *
     * @return SubCommandConfig The sub-command configuration.
     *
     * @see editSubCommand()
     */
    public function beginSubCommand($name)
    {
        $config = new SubCommandConfig($name, $this);

        // The name is dynamic, so don't store by name
        $this->subCommandConfigs[] = $config;

        return $config;
    }

    /**
     * Alias for {@link getSubCommandConfig()}.
     *
     * This method can be used to nicely edit a sub-command inherited from a
     * parent configuration using the fluent API:
     *
     * ```php
     * protected function configure()
     * {
     *     parent::configure();
     *
     *     $this
     *         ->editCommand('server')
     *             ->editSubCommand('add')
     *                 // ...
     *             ->end()
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * @param string $name The name of the sub-command to edit.
     *
     * @return SubCommandConfig The sub-command configuration.
     *
     * @see beginSubCommand()
     */
    public function editSubCommand($name)
    {
        return $this->getSubCommandConfig($name);
    }

    /**
     * Starts a configuration block for an option command.
     *
     * An option command is executed if the corresponding option is passed after
     * the command name. For example, if the command "server" has an option
     * command named "--add" with the short name "-a", that command can be
     * called with:
     *
     * ```
     * $ console server --add ...
     * $ console server -a ...
     * ```
     *
     * The configuration of the option command is returned by this method.
     * You can use the fluent interface to configure the option command
     * before jumping back to this configuration with
     * {@link SubCommandConfig::end()}:
     *
     * ```php
     * protected function configure()
     * {
     *     $this
     *         ->beginCommand('server')
     *             ->setDescription('List and manage servers')
     *
     *             ->beginOptionCommand('add', 'a')
     *                 ->setDescription('Add a server')
     *                 ->addArgument('host', Argument::REQUIRED)
     *                 ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80)
     *             ->end()
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * @param string $name      The name of the option command.
     * @param string $shortName The short name of the option command.
     *
     * @return OptionCommandConfig The option command configuration.
     *
     * @see editOptionCommand()
     */
    public function beginOptionCommand($name, $shortName = null)
    {
        $config = new OptionCommandConfig($name, $shortName, $this);

        // The name is dynamic, so don't store by name
        $this->subCommandConfigs[] = $config;

        return $config;
    }

    /**
     * Alias for {@link getSubCommandConfig()}.
     *
     * This method can be used to nicely edit an option command inherited from a
     * parent configuration using the fluent API:
     *
     * ```php
     * protected function configure()
     * {
     *     parent::configure();
     *
     *     $this
     *         ->editCommand('server')
     *             ->editOptionCommand('add')
     *                 // ...
     *             ->end()
     *         ->end()
     *
     *         // ...
     *     ;
     * }
     * ```
     *
     * @param string $name The name of the option command to edit.
     *
     * @return OptionCommandConfig The option command configuration.
     *
     * @see beginOptionCommand()
     */
    public function editOptionCommand($name)
    {
        return $this->getSubCommandConfig($name);
    }

    /**
     * Adds configuration for a sub-command.
     *
     * @param SubCommandConfig $config The sub-command configuration.
     *
     * @return static The current instance.
     *
     * @see beginSubCommand()
     */
    public function addSubCommandConfig(SubCommandConfig $config)
    {
        // The name is dynamic, so don't store by name
        $this->subCommandConfigs[] = $config;

        $config->setParentConfig($this);

        return $this;
    }

    /**
     * Adds sub-command configurations to the command.
     *
     * @param SubCommandConfig[] $configs The sub-command configurations.
     *
     * @return static The current instance.
     *
     * @see beginSubCommand()
     */
    public function addSubCommandConfigs(array $configs)
    {
        foreach ($configs as $command) {
            $this->addSubCommandConfig($command);
        }

        return $this;
    }

    /**
     * Sets the sub-command configurations of the command.
     *
     * @param SubCommandConfig[] $configs The sub-command configurations.
     *
     * @return static The current instance.
     *
     * @see beginSubCommand()
     */
    public function setSubCommandConfigs(array $configs)
    {
        $this->subCommandConfigs = array();

        $this->addSubCommandConfigs($configs);

        return $this;
    }

    /**
     * Returns the sub-command configuration for a given name.
     *
     * @param string $name The name of the sub-command.
     *
     * @return SubCommandConfig The sub-command configuration.
     *
     * @throws NoSuchCommandException If the sub-command configuration is not
     *                                found.
     *
     * @see beginSubCommand()
     */
    public function getSubCommandConfig($name)
    {
        foreach ($this->subCommandConfigs as $commandConfig) {
            if ($name === $commandConfig->getName()) {
                return $commandConfig;
            }
        }

        throw NoSuchCommandException::forCommandName($name);
    }

    /**
     * Returns the configurations of all sub-commands.
     *
     * @return SubCommandConfig[] The sub-command configurations.
     *
     * @see beginSubCommand()
     */
    public function getSubCommandConfigs()
    {
        return $this->subCommandConfigs;
    }

    /**
     * Returns whether the command has a sub-command with a given name.
     *
     * @param string $name The name of the sub-command.
     *
     * @return bool Returns `true` if the sub-command configuration with the
     *              given name exists and `false` otherwise.
     *
     * @see beginSubCommand()
     */
    public function hasSubCommandConfig($name)
    {
        foreach ($this->subCommandConfigs as $commandConfig) {
            if ($name === $commandConfig->getName()) {
                return true;
            }
        }

        return false;
    }

    /**
     * Returns whether the command has any registered sub-command configurations.
     *
     * @return bool Returns `true` if sub-command configurations were added to
     *              the command and `false` otherwise.
     *
     * @see beginSubCommand()
     */
    public function hasSubCommandConfigs()
    {
        return count($this->subCommandConfigs) > 0;
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultHelperSet()
    {
        return $this->applicationConfig
            ? $this->applicationConfig->getHelperSet()
            : parent::getDefaultHelperSet();
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultHandler()
    {
        return $this->applicationConfig
            ? $this->applicationConfig->getHandler()
            : parent::getDefaultHandler();
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultHandlerMethod()
    {
        return $this->applicationConfig
            ? $this->applicationConfig->getHandlerMethod()
            : parent::getDefaultHandlerMethod();
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultArgsParser()
    {
        return $this->applicationConfig
            ? $this->applicationConfig->getArgsParser()
            : parent::getDefaultArgsParser();
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultLenientArgsParsing()
    {
        return $this->applicationConfig
            ? $this->applicationConfig->isLenientArgsParsingEnabled()
            : parent::getDefaultLenientArgsParsing();
    }
}
Webmozart Console API API documentation generated by ApiGen