Skip to content
Yo-An Lin edited this page Jul 20, 2015 · 10 revisions

Tutorial

Add cliframework to your composer.json:

{
   "require": {
      "corneltek/cliframework": "~2.6"
   }
}

Or, you can run the command below to install the dependencies:

composer require "corneltek/cliframework:~2.6"

To use CLIFramework, please define the application class first,

src/YourApp/CLIApplication.php:

namespace YourApp;
use CLIFramework\Application;

class CLIApplication extends Application
{

    /* init your application options here */
    public function options($opts)
    {
        $opts->add('v|verbose', 'verbose message');
        $opts->add('path:', 'required option with a value.');
        $opts->add('path?', 'optional option with a value');
        $opts->add('path+', 'multiple value option.');
    }

    /* register your command here */
    public function init()
    {
        $this->command( 'list', '\YourApp\Command\ListCommand' );
        $this->command( 'foo', '\YourApp\Command\FooCommand' );
        $this->command( 'bar' );    // initialize with \YourApp\Command\BarCommand
    }

}

Then define your command class:

src/YourApp/Command/ListCommand.php:

namespace YourApp\Command;
use CLIFramework\Command;
class ListCommand extends Command {

    function init()
    {
        // register your subcommand here ..
    }

    function options($opts)
    {
        // command options

    }

    function execute($arg1,$arg2,$arg3 = 0)
    {
        $logger = $this->logger;

        $logger->info('execute');
        $logger->error('error');

        $input = $this->ask('Please type something');

    }
}

To start your application, create a new script file named bin/yourapp:

require 'vendor/autoload.php';
$app = new \YourApp\Application;
$app->runWithTry($argv);

To test your application:

php bin/yourapp