A sub-command is defined within the scope of another command. For example, in the command server add , the command "add" is a sub-command of the "server" command.
See also: OptionCommandConfig
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends CommandConfig
 public function testLenientArgsParsingDefaultsToParentValue()
 {
     $this->parentConfig->enableLenientArgsParsing();
     $this->assertTrue($this->config->isLenientArgsParsingEnabled());
     $this->parentConfig->disableLenientArgsParsing();
     $this->assertFalse($this->config->isLenientArgsParsingEnabled());
 }
 /**
  * Sets the name of the command.
  *
  * Contrary to the base implementation, the name of an option command must
  * contain at least two characters.
  *
  * @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::greaterThan(strlen($name), 1, sprintf('The command name should contain at least two characters. Got: "%s"', $name));
     }
     parent::setName($name);
     return $this;
 }
 public function testHasSubCommandConfig()
 {
     $this->config->addSubCommandConfig($config = new SubCommandConfig());
     $this->assertFalse($this->config->hasSubCommandConfig('command'));
     $this->assertFalse($this->config->hasSubCommandConfig('foobar'));
     $config->setName('command');
     $this->assertTrue($this->config->hasSubCommandConfig('command'));
     $this->assertFalse($this->config->hasSubCommandConfig('foobar'));
 }
Beispiel #4
0
 public function testHasDefaultSubCommands()
 {
     $config = new CommandConfig('command');
     $subConfig = new SubCommandConfig('sub');
     $subConfig->markDefault();
     $config->addSubCommandConfig($subConfig);
     $command = new Command($config);
     $this->assertTrue($command->hasDefaultSubCommands());
 }
Beispiel #5
0
 /**
  * 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;
 }
Beispiel #6
0
 private function validateSubCommandName(SubCommandConfig $config)
 {
     $name = $config->getName();
     if (!$name) {
         throw CannotAddCommandException::nameEmpty();
     }
     if ($this->subCommands->contains($name)) {
         throw CannotAddCommandException::nameExists($name);
     }
     if ($config instanceof OptionCommandConfig) {
         if ($this->argsFormat->hasOption($name)) {
             throw CannotAddCommandException::optionExists($name);
         }
         if ($shortName = $config->getShortName()) {
             if ($this->subCommands->contains($shortName)) {
                 throw CannotAddCommandException::nameExists($name);
             }
             if ($this->argsFormat->hasOption($shortName)) {
                 throw CannotAddCommandException::optionExists($shortName);
             }
         }
     }
 }