Cling is a Micro Framework for building Command Line Applications
- Routing of Command Line Arguments
- Short and Long Arguments
- Automagic
--help
creation - Configuration
As Cling is very new, there's still lot of work left to be done
- Testing
- Template System
- Logging
- Proper Error and Eception Handling
- The :stdin command needs to check if there is data coming in on
- PHP 5.3
<?php
require 'Cling/Cling.php';
$app = new Cling();
$app->command('hello-world:', function($name) {
echo "Hello $name\n";
})->help("Hello World Example Command.");
$app->run();
To configue Cling, you can pass an associative array to the constructor method. The Options are:
- debug Enable or Disable debug output (Default: false)
A Command Line Option can be handled by the command
function:
$app->command('<Long Option Name>', '<1 letter short option'>, '<callable>');
The Short Option parameter is optional.
A :*
command is a Route that is executed regardless of Command Line Options. Multiple :*
Routes are executed in the order they are set.
If a user calls the application without specifying a valid command line option, the notFound handler is called. You can override the default handler, by calling the notFound method with a user function:
$app = new Cling();
$app->notFound(function() use ($app) {
echo "I can't help you...\n";
});
The application will exit
immediatle if the notFound method is being called.
Console Applications should, just like a Website, facilitate Views:
$app = new Cling(array('template.path' => 'views/'));
$app->command('hello-world:', function($name) use ($app) {
$app->view()->set('name', $name);
$app->view()->display('example.txt.php')
})->help("Example with a View.");
The view would look then something like:
Hello <?php echo $name; ?>!
I Hope You're doing fine.
To Log message to files, stdout or stderr:
$app = new Cling(array('log.destination' => \Cling\Logger::LOG_STDOUT));
$app->log()->info('Hello World!');
- \Cling\Logger::LOG_STDOUT => Log to STDOUT
- \Cling\Logger::LOG_STDERR => Log to STDERR
- \Cling\Logger::LOG_FILE => Log to a file in a directory set by 'log.dir'
- \Cling\Logger::LOG_ERROR_LOG => Log via Default 'error_log'
The Following Log Levels Are available
- emerg => Emergency: system is unusable
- alert => Alert: action must be taken immediately
- crit => Critical: critical conditions
- error => Error: error conditions
- warn => Warning: warning conditions
- notice => Notice: normal but significant condition
- info => Informational: informational messages
- debug => Debug: debug messages