CLI args

I'm trying to get the controller/method/args passed through to a command line script. In the following discussion it mentions a change in 1.1.0 which means you need to do something different. It doesn't, however, describe what it is you need to do. Can anyone help shed some light on what is needed to get the example code to work?

http://forum.phalconphp.com/discussion/277/cli-loads-only-maintask-class-mainaction-method

Many thanks, George



2.0k

Yes, I saw that, I'm just not sure how to apply that to the following:

$app = new \Phalcon\CLI\Console(); $app->setDI($di); $app->handle($_SERVER['argv']);

In other words, I can set the handle manually as per the unit tests, but I can't seem to work out how to get it to read in commands from the command line.

You need explicitly set what parameter is the task/module/action, etc:

$app->handle([
   'task' => $_SERVER['argv'][0]
   'action' => $_SERVER['argv'][1]
]);


2.0k

Doesn't seems like a better solution, what if the user doesn't give all arguments? How do you handle the case eg:

'module' => 'devtools',
'task' => 'main',
'action' => 'hello',
'World',
'######'

Where the action takes potentially multiple input.

Is there some kind of parser?

This works for me atm. All tasks in one directory and without modules:

$loader = new \Phalcon\Loader();

$loader->registerDirs(array(
        __DIR__ .'/tasks/'
))->register();

//Using the CLI factory default services container
$di = new \Phalcon\DI\FactoryDefault\CLI();

//Create a console application
$console = new \Phalcon\CLI\Console();
$console->setDI($di);

$args = array();
$argv = $_SERVER['argv'];

$args['task'] = isset($argv[1]) ? $argv[1] : 'main';
$args['action'] = isset($argv[2]) ? $argv[2] : 'main';

unset($argv[0], $argv[1], $argv[2]);

$args['params'] = count($argv) > 0 ? array_values($argv) : array();

$console->handle($args);

Maybe this helps: http://forum.phalconphp.com/discussion/372/phalcon-cli-apps-with-namespaces-in-phalcon-loader#C1606

But I used it myself also a bit different. I just registered different directories with unique task names:

            $loader = new Phalcon\Loader();
            $loader
                // FIXME: Loader needs to runs with namespaces
                //->registerNamespaces($this->_taskDirectories)
                ->registerDirs($this->_taskDirectories)
                ->register();