Re: Command line PHP
| From: | TR Shaw | Date: | Fri, 07 Jan 2011 19:34:54 +0000 |
| Subject: | Re: Command line PHP | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310572@lists.php.net to get a copy of this message | ||
On Jan 7, 2011, at 12:08 PM, Nicholas Kell wrote:
>
> On Jan 7, 2011, at 11:01 AM, Joshua Kehn wrote:
>
>> On Jan 7, 2011, at 11:55 AM, larry@garfieldtech.com wrote:
>>
>>> Hi folks. I have a project coming up that will involve writing a non-trivial command
>>> line PHP application. Most of it will be nice and abstracted and standalone and all of that jazz,
>>> but it will need to do command line interation. I'm not sure yet if it will be interactive or
>>> if I just need to parse lots of command line switches.
>>>
>>> Has anyone used a CLI-handling library they like? I recall briefly using the PEAR CLI
>>> library many many years ago and disliking it because it was only barely a step above the raw PHP-CLI
>>> SAPI, and still required lots of if-else branching in my code. I don't know if there's
>>> anything better since then, however. I prefer clean OO to procedural, but can work with procedural
>>> if needs be. The fewer dependencies it has the better as well.
>>>
>>> Any recommendations?
>>>
>>> (Open source, GPLv2-compatible required.)
>>>
>>> --Larry Garfield
>>
>> Larry-
>>
>> Why are you writing a command line application in PHP? I would think that is starting off
>> on a very wrong foot.
>>
>> Can you explain the requirements / use cases a bit more?
>>
>
> I agree. Not saying that it isn't doable, because it certainly is, but there may be other
> languages that are available and easier to implement a CLI app, such as Python, or some other QnD
> scripting language.
Pardon me but what's the big deal?
Put a shebang up top and then add:
//
// ----------------------------------------------------------
// Get CLI args from argv
// ----------------------------------------------------------
//
function getargs($argv)
{
// ./cli.php file1 file2 --path=/foo/bar -t -B quux
// getargs($GLOBALS['argv'])
$args['self'] = array_shift( $argv ) ;
while( 0 < sizeof( $argv ) ) {
$arg = array_shift( $argv ) ;
if( '--' == substr( $arg , 0 , 2 ) ) {
$eq = strpos( $arg , '=' ) ;
$name = substr( $arg , 2 , $eq - 2 ) ;
$val = substr( $arg , $eq + 1 ) ;
$args[$name][] = $val ;
} else if( '-' == $arg[0] ) {
$name = substr( $arg , 1 ) ;
if (isset($argv[0][0]) && ( '-' != $argv[0][0] )) {
$val = array_shift( $argv ) ;
} else {
$val = '' ;
}
$args[$name][] = $val ;
} else {
$args['file'][] = substr( $arg , 0 ) ;
}
}
return $args ;
}
// ----------------------------------------------------------
// Main
// ----------------------------------------------------------
//Get CLI options
$args = getargs($GLOBALS['argv']);
//Display Basic Help
if (isset($args['h']) || isset($args['help'])) {
echo "./your_php_cli_app.php -h ....\n";
echo " -h - displays this help\n";
echo " ... is more of your command line args\n";
echo "\n";
exit;
}