Reputation: 3301
Here is what I use to build a php command line applications to process data.
These applications are not web applications, they run in command line shell to process some data.
Due to the Legacy reason, I use Laravel php framework.
So I use Laravel "Artisan Development" to build custom commands and then run the commands use
php artisan ...
Laravel is for a web app, but now, I am only using its artisan commands. It seems an overkill, that I install the full version Laravel Framework for this purpose.
But I could not find a good php micro-framework specially to building commands application.
My questions are
Thanks!
Upvotes: 0
Views: 177
Reputation: 21661
I made this small library,
https://github.com/ArtisticPhoenix/Cli
Your welcome to use it Or get inspiration from it, basically its a wrapper around getopt()
http://php.net/manual/en/function.getopt.php
getopt — Gets options from the command line argument list
You can get it on composer too:
"require" : {
"evo/cli" : "~1.0"
}
Basic usage is like this:
$Cli = Cli::getInstance();
$Cli->setArgument('h', 'help', 'Show this help document');
//... other arguments
if($Cli->getArgument('h')) $Cli->printHelpDoc(); //exits
And so on.
You can even do it from a PHP config file, which just gets put into setArgument()
~ basically.
//config.php
return [
[
'shortName' => 'h',
'longName' => 'help',
'doc' => 'Show this help document'
]
];
Then
//cli.php
$config = require 'config.php';
$Cli = Cli::getInstance();
$Cli->fromConfig($config);
if($Cli->getArgument('h')) $Cli->printHelpDoc(); //exits
You can also do dynamic validation of the input args like this (with a closure):
$Cli->setArgument('f', 'foo', 'This is just foo, and must always be foo', [
'accept' => function($shortName, $value){
if($value == 'foo') return true;
return false;
}
]);
The above just returns a Boolean, if the value is good or not. If you return false it will issue an exception, etc. You can also make an argument required like this:
$Cli->setArgument('i', 'input', 'This is input that requires a value', [
'requireValue' => true
]);
And of course you can combine those 2.
There is some documentation on the Github page. It has a few small dependencies, just common stuff that I like to re-use (all in composer). Together it's less then 1000 lines of code. Maybe 20 or 30kb.
I needed to make a small command line thing, and I had this idea...
Upvotes: 1
Reputation: 2533
For Python, I definitely recommend Click. It has become the Python library for creating command line tools. It's written/maintained by the author of Flask (currently the #5 most-starred Python repo on GitHub). I use Click all the time when creating command line tools. It has everything most people would need.
Not sure what your definition of a micro-framework is, but Click has no external dependencies and takes up approx. 500 KB (including .pyc files) when I install it. Also, it doesn't really require any boilerplate to setup and start using. So I guess I would call it a micro-framework.
Upvotes: 1