Skip to content

Latest commit

 

History

History
101 lines (74 loc) · 3.35 KB

README.md

File metadata and controls

101 lines (74 loc) · 3.35 KB

PimpleCli

Build Status Latest Stable Version License Total Downloads Scrutinizer Code Quality

PimpleCli on Packagist

PimpleCli is a tool that makes it easy creating command line application.

PimpleCli works with a Pimple container (eg: a Silex 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.

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 to get started.

Installation

Through Composer :

{
    "require": {
        "gitory/pimple-cli": "~1.0"
    }
}

Examples

Silex 2

composer.json

{
    "require": {
        "silex/silex": "~2.0@dev",
        "gitory/pimple-cli": "~1.0",
        "symfony/console": "~2.0"
    }
}

GreetCommand.php

namespace Acme\DemoBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GreetCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('demo:greet')
            ->setDescription('Greet someone')
            ->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        $text = 'Hello '.$name;
        $output->writeln($text);
    }
}

index.php

require_once __DIR__.'/vendor/autoload.php';
require_once 'GreetCommand.php';

$silexApp = new Silex\Application();
$silexApp->register(new Gitory\PimpleCli\ServiceCommandServiceProvider());

// add your command as services ending in '.command' in your DI
$silexApp['user.new.command'] = function () {
    return new Acme\DemoBundle\Command\GreetCommand();
};

$consoleApp = new Symfony\Component\Console\Application();
$consoleApp->addCommands($silexApp['command.resolver']->commands());
$consoleApp->run();

Launch in Cli : php index.php demo:greet John

command command