Returns the console application.
public getApplication ( ) : Webmozart\Console\Api\Application\Application | ||
return | Webmozart\Console\Api\Application\Application | The console application. |
/** * {@inheritdoc} */ public function handle(Args $args, IO $io, Command $command) { $application = $command->getApplication(); if ($args->isArgumentSet('command')) { $theCommand = $application->getCommand($args->getArgument('command')); $usage = new CommandHelp($theCommand); } else { $usage = new ApplicationHelp($application); } $usage->render($io); return 0; }
/** * {@inheritdoc} */ public function handle(Args $args, IO $io, Command $command) { $descriptor = new XmlDescriptor(); $output = new IOOutput($io); $application = $command->getApplication(); $applicationAdapter = new ApplicationAdapter($application); if ($args->isArgumentSet('command')) { $theCommand = $application->getCommand($args->getArgument('command')); $commandAdapter = new CommandAdapter($theCommand, $applicationAdapter); $descriptor->describe($output, $commandAdapter); } else { $descriptor->describe($output, $applicationAdapter); } return 0; }
public function testCreate() { $config = new CommandConfig('command'); $config->addArgument('argument'); $config->addOption('option', 'o'); $command = new Command($config, $this->application); $this->assertSame($config, $command->getConfig()); $this->assertSame($this->application, $command->getApplication()); $argsFormat = $command->getArgsFormat(); $this->assertNull($argsFormat->getBaseFormat()); $this->assertCount(1, $argsFormat->getArguments()); $this->assertTrue($argsFormat->hasArgument('argument')); $this->assertCount(1, $argsFormat->getOptions()); $this->assertTrue($argsFormat->hasOption('option')); }
/** * Renders the "Usage" section. * * @param BlockLayout $layout The layout. * @param Command $command The command to render. */ protected function renderUsage(BlockLayout $layout, Command $command) { $formatsToPrint = array(); // Start with the default commands if ($command->hasDefaultSubCommands()) { // If the command has default commands, print them foreach ($command->getDefaultSubCommands() as $subCommand) { // The name of the sub command is only optional (i.e. printed // wrapped in brackets: "[sub]") if the command is not // anonymous $nameOptional = !$subCommand->getConfig()->isAnonymous(); $formatsToPrint[] = array($subCommand->getArgsFormat(), $nameOptional); } } else { // Otherwise print the command's usage itself $formatsToPrint[] = array($command->getArgsFormat(), false); } // Add remaining sub-commands foreach ($command->getSubCommands() as $subCommand) { // Don't duplicate default commands if (!$subCommand->getConfig()->isDefault()) { $formatsToPrint[$subCommand->getName()] = array($subCommand->getArgsFormat(), false); } } $appName = $command->getApplication()->getConfig()->getName(); $prefix = count($formatsToPrint) > 1 ? ' ' : ''; $layout->add(new Paragraph('<b>USAGE</b>')); $layout->beginBlock(); foreach ($formatsToPrint as $vars) { $this->renderSynopsis($layout, $vars[0], $appName, $prefix, $vars[1]); $prefix = 'or: '; } if ($command->hasAliases()) { $layout->add(new EmptyLine()); $this->renderAliases($layout, $command->getAliases()); } $layout->endBlock(); $layout->add(new EmptyLine()); }
/** * Callback for selecting the handler that should be run. * * @param Args $args The console arguments. * @param IO $io The I/O. * @param Command $command The handled command. * * @return string The name of the handler to run. */ public function getHandlerToRun(Args $args, IO $io, Command $command) { $rawArgs = $args->getRawArgs(); // The raw arguments should always be available, but check anyway if (!$rawArgs) { return 'text'; } // If "-h" is given, always print the short text usage if ($rawArgs->hasToken('-h')) { return 'text'; } // Check if any of the options is set foreach ($this->getRegisteredNames() as $handlerName) { if ($rawArgs->hasToken('--' . $handlerName)) { return $handlerName; } } // No format option is set, "-h" is not set // If a command is given or if "--help" is set, display the manual if ($rawArgs->hasToken('--help')) { // Return "man" if the binary is available and the man page exists // The process launcher must be supported on the system $manPage = $this->getManPage($command->getApplication(), $args); if (file_exists($manPage) && $this->processLauncher->isSupported()) { if (!$this->manBinary) { $this->manBinary = $this->executableFinder->find('man'); } if ($this->manBinary) { return 'man'; } } // Return "ascii-doc" if the AsciiDoc page exists $asciiDocPage = $this->getAsciiDocPage($command->getApplication(), $args); if (file_exists($asciiDocPage)) { return 'ascii-doc'; } } // No command, no option -> display command list as text return 'text'; }