|
| 1 | +# PimpleCli |
| 2 | + |
| 3 | +[](https://travis-ci.org/Gitory/pimple-cli) |
| 4 | +[](https://packagist.org/packages/gitory/pimple-cli) |
| 5 | +[](https://packagist.org/packages/gitory/pimple-cli) |
| 6 | +[](https://packagist.org/packages/gitory/pimple-cli) |
| 7 | +[](https://scrutinizer-ci.com/g/Gitory/pimple-cli/?branch=master) |
| 8 | + |
| 9 | +[PimpleCli on Packagist](https://packagist.org/packages/gitory/pimple-cli) |
| 10 | + |
| 11 | +PimpleCli is a tool that makes it easy creating command line application. |
| 12 | + |
| 13 | +PimpleCli works with a [Pimple](http://pimple.sensiolabs.org/) container (eg: a [Silex](http://silex.sensiolabs.org) application) and a Console Application (eg: using http://symfony.com/doc/current/components/console/introduction.html). PimpleCli's role is to discover commands in the Pimple container to make them availlable the the Console Application. |
| 14 | + |
| 15 | +Commands needs to be registered as a service with a name ending in '.command'. The command can be anything (class, callable, etc.) that the console application understand. When using the `Symfony\Component\Console\Application` command should extends `Symfony\Component\Console\Command\Command`. You can take a look at the [Console Components documentation](http://symfony.com/doc/current/components/console/introduction.html) to get started. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Through Composer : |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "require": { |
| 24 | + "gitory/pimple-cli": "~1.0" |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +### Silex 2 |
| 32 | + |
| 33 | +**composer.json** |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "require": { |
| 38 | + "silex/silex": "~2.0@dev", |
| 39 | + "gitory/pimple-cli": "~1.0", |
| 40 | + "symfony/console": "~2.0" |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +**GreetCommand.php** |
| 48 | + |
| 49 | +```php |
| 50 | +namespace Acme\DemoBundle\Command; |
| 51 | + |
| 52 | +use Symfony\Component\Console\Command\Command; |
| 53 | +use Symfony\Component\Console\Input\InputArgument; |
| 54 | +use Symfony\Component\Console\Input\InputInterface; |
| 55 | +use Symfony\Component\Console\Input\InputOption; |
| 56 | +use Symfony\Component\Console\Output\OutputInterface; |
| 57 | + |
| 58 | +class GreetCommand extends Command |
| 59 | +{ |
| 60 | + protected function configure() |
| 61 | + { |
| 62 | + $this |
| 63 | + ->setName('demo:greet') |
| 64 | + ->setDescription('Greet someone') |
| 65 | + ->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?') |
| 66 | + ; |
| 67 | + } |
| 68 | + |
| 69 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 70 | + { |
| 71 | + $name = $input->getArgument('name'); |
| 72 | + $text = 'Hello '.$name; |
| 73 | + $output->writeln($text); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +**index.php** |
| 79 | + |
| 80 | +```php |
| 81 | +require_once __DIR__.'/vendor/autoload.php'; |
| 82 | +require_once 'GreetCommand.php'; |
| 83 | + |
| 84 | +$silexApp = new Silex\Application(); |
| 85 | +$silexApp->register(new Gitory\PimpleCli\ServiceCommandServiceProvider()); |
| 86 | + |
| 87 | +// add your command as services ending in '.command' in your DI |
| 88 | +$silexApp['user.new.command'] = function () { |
| 89 | + return new Acme\DemoBundle\Command\GreetCommand(); |
| 90 | +}; |
| 91 | + |
| 92 | +$consoleApp = new Symfony\Component\Console\Application($silexApp); |
| 93 | +$consoleApp->addCommands($silexApp['command.resolver']->commands()); |
| 94 | +$consoleApp->run(); |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +Launch in Cli : `php index.php demo:greet John` |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments