Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active August 29, 2015 14:03
Show Gist options
  • Save ngyuki/fadaefec09f5bd05d280 to your computer and use it in GitHub Desktop.
Save ngyuki/fadaefec09f5bd05d280 to your computer and use it in GitHub Desktop.
symfony console single
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyApplication extends Application
{
private function getCalleeName()
{
global $argv;
return basename($argv[0]);
}
protected function getCommandName(InputInterface $input)
{
return $this->getCalleeName();
}
protected function getDefaultCommands()
{
$commands = parent::getDefaultCommands();
$commands[] = (new Command($this->getCalleeName()))
->setDescription("Hello")
->addOption("name", "N", InputOption::VALUE_OPTIONAL, "Name", "unknown")
->setCode(function (InputInterface $input, OutputInterface $output) {
$name = $input->getOption('name');
$output->writeln("Hello <info>$name</info>");
})
;
return $commands;
}
public function getDefinition()
{
$definition = parent::getDefinition();
$definition->setArguments();
return $definition;
}
}
(new MyApplication())->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment