-
Notifications
You must be signed in to change notification settings - Fork 10
CLI
You can define a command line interface via "command routes".
Similar to the Route module, the CLI module is responsible for this feature.
Create a new file and give execution permissions:
$ touch myapp && chmod +x myappWrite this stub into myapp file :
#!/usr/bin/env php
<?php
// Load Core and vendors
include 'vendor/autoload.php';
// Define commands routes here...
// Run the CLI dispatcher
CLI::run();CLI routes are defined by whitespace separated fragments.
CLI::on('hello',function(){
echo "Hello, friend.",PHP_EOL;
});$ ./myapp hello
Hello, friend.Other "static" parameters, if passed are required for the command execution.
CLI::on('hello friend',function(){
echo "Hello, friend.",PHP_EOL;
});$ ./myapp hello
Error: Command [hello] is incomplete.
$ ./myapp hello friend
Hello, friend.You can extract parameter from the route by prefixing the fragment name by a semicolon ":". Extracted fragments are required and will be passed to the route callback by left-to-right position.
CLI::on('hello :name',function($name){
echo "Hello, $name.",PHP_EOL;
});$ ./myapp hello
Error: Command [hello] needs more parameters.
$ ./myapp hello "Gordon Freeman"
Hello, Gordon Freeman.Options are position free parameters, they can be passed everywhere in the command route and are optional.
You can retrieve their value with the CLI::input($name, $default = null) method.
CLI::on('process :filename',function($filename){
$optimize = CLI::input('optimize',false);
$outputfile = CLI::input('output',$filename.'.out');
$data = file_get_contents($filename);
/* process $data */
if ($optimize) { /* optimize data */ };
file_put_contents($outputfile,$data);
});./myapp process --optimize ./test.html --output=test_opt.htmlIf you don't pass an argument for an option --optimize, the true value will be used.
Core is maintained by using the Semantic Versioning Specification (SemVer).
Copyright 2014-2016 Caffeina srl under the MIT license.
http://caffeina.com