Implementations of this class represent the arguments that a user passes when calling the console. For example: $ console server add --port 80 localhost In this case, the raw arguments contain the tokens: * "server" * "add" * "--port" * "80" * "localhost" With an implementation of {@link ArgsParser} and a configured {@link ArgsFormat}, the {@link RawArgs} instance can be converted into an {@link Args} instance: php $format = ArgsFormat::build() ->addCommandName(new CommandName('server')) ->addCommandName(new CommandName('add')) ->addOption(new Option('port', 'p', Option::VALUE_REQUIRED | Option::INTEGER)) ->addArgument(new Argument('host', Argument::REQUIRED)) ->getFormat(); $args = $parser->parseArgs($rawArgs, $format); The {@link Args} instance can be used to access the options and arguments of a command in a convenient way.
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
 /**
  * @param Application       $application
  * @param RawArgs           $args
  * @param InputStream|null  $inputStream
  * @param OutputStream|null $outputStream
  * @param OutputStream|null $errorStream
  *
  * @return ConsoleIO
  */
 public function createIO(Application $application, RawArgs $args, InputStream $inputStream = null, OutputStream $outputStream = null, OutputStream $errorStream = null)
 {
     $inputStream = $inputStream ?: new StandardInputStream();
     $outputStream = $outputStream ?: new StandardOutputStream();
     $errorStream = $errorStream ?: new ErrorOutputStream();
     $styleSet = $application->getConfig()->getStyleSet();
     if ($args->hasToken('--no-ansi')) {
         $outputFormatter = $errorFormatter = new PlainFormatter($styleSet);
     } elseif ($args->hasToken('--ansi')) {
         $outputFormatter = $errorFormatter = new AnsiFormatter($styleSet);
     } else {
         $outputFormatter = $outputStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet);
         $errorFormatter = $errorStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet);
     }
     $io = new ConsoleIO(new Input($inputStream), new Output($outputStream, $outputFormatter), new Output($errorStream, $errorFormatter));
     if ($args->hasToken('-vvv') || $this->isDebug()) {
         $io->setVerbosity(IO::DEBUG);
     } elseif ($args->hasToken('-vv')) {
         $io->setVerbosity(IO::VERY_VERBOSE);
     } elseif ($args->hasToken('-v')) {
         $io->setVerbosity(IO::VERBOSE);
     }
     if ($args->hasToken('--quiet') || $args->hasToken('-q')) {
         $io->setQuiet(true);
     }
     if ($args->hasToken('--no-interaction') || $args->hasToken('-n')) {
         $io->setInteractive(false);
     }
     return $io;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function getParameterOption($values, $default = false, $onlyParams = false)
 {
     $tokens = $this->rawArgs->getTokens();
     foreach ((array) $values as $value) {
         for (reset($tokens); null !== key($tokens); next($tokens)) {
             $token = current($tokens);
             if ($onlyParams && $token === '--') {
                 // end of options (--) signal reached, stop now
                 return $default;
             }
             // Long/short option with value in the next argument
             if ($token === $value) {
                 $next = next($tokens);
                 return $next && $next !== '--' ? $next : null;
             }
             // Long option with =
             if (0 === strpos($token, $value . '=')) {
                 return substr($token, strlen($value) + 1);
             }
             // Short option
             if (strlen($token) > 2 && '-' === $token[0] && '-' !== $token[1] && 0 === strpos($token, $value)) {
                 return substr($token, 2);
             }
         }
     }
     return $default;
 }
 /**
  * {@inheritdoc}
  */
 public function resolveCommand(RawArgs $args, Application $application)
 {
     $tokens = $args->getTokens();
     $namedCommands = $application->getNamedCommands();
     $argumentsToTest = $this->getArgumentsToTest($tokens);
     $optionsToTest = $this->getOptionsToTest($tokens);
     // Try to find a command for the passed arguments and options.
     if ($result = $this->processArguments($args, $namedCommands, $argumentsToTest, $optionsToTest)) {
         return $this->createResolvedCommand($result);
     }
     // If arguments were passed, we did not find the matching command.
     if ($argumentsToTest) {
         throw CannotResolveCommandException::nameNotFound(reset($argumentsToTest), $namedCommands);
     }
     // If no arguments were passed, run the application's default command.
     if ($result = $this->processDefaultCommands($args, $application->getDefaultCommands())) {
         return $this->createResolvedCommand($result);
     }
     // No default command is configured.
     throw CannotResolveCommandException::noDefaultCommand();
 }
 /**
  * {@inheritdoc}
  */
 public function parseArgs(RawArgs $args, ArgsFormat $format, $lenient = false)
 {
     $this->setTokens($args->getTokens());
     $formatAdapter = new ArgsFormatInputDefinition($format);
     try {
         $this->bind($formatAdapter);
     } catch (RuntimeException $e) {
         if (!$lenient) {
             throw new CannotParseArgsException($e->getMessage());
         }
     }
     // Prevent failing validation if not all command names are given
     $this->insertMissingCommandNames($formatAdapter, $lenient);
     try {
         $this->validate();
     } catch (RuntimeException $e) {
         if (!$lenient) {
             throw new CannotParseArgsException($e->getMessage());
         }
     }
     return $this->createArgs($format, $args);
 }
Exemple #5
0
 /**
  * Returns the PHP script as it was called on the console.
  *
  * @return string|null The script name or null if no script name is
  *                     available.
  */
 public function getScriptName()
 {
     return $this->rawArgs ? $this->rawArgs->getScriptName() : null;
 }