OLIVER WILKERSON

PHP CLI sample (argument processing without getopts)

PHP CLI sample (argument processing without getopts)
Feb 01, 2012 - 8:37:51 pm

I wanted a more versatile way to parse arguments in php, so I threw this one together. (This is a follow-up to my previous post.) Parsing arguments in php with getopts and such is fine, but if you want unix-like or even windows-like delimiting there's a bunch of funny logic that can go along with that. Re-writing an option parser from scratch every time you need one is well... dumb.

Admittedly, this option parser isn't as straight-forward as the use of boost in C++, but I think it sticks if you use it once or twice. (I wrote it, so how would I know?)

At the beginning of your php script

$app_name = array_shift($argv);
$arguments = array();
$arg_names = array(
    "-h" => "--help",
    "-t:" => "--test:"
);
$arg_count = count($argv);
for ($i=0; $i < $arg_count; $i++) { 
    $arg = $argv[$i];
    $is_arg = strpos($arg, '-') == 0;
    if ($is_arg) {
        foreach ($arg_names as $arg_name => $arg_long_name) {
            $expects_value = strpos($arg_name, ':') !== false;
            $eq_pos = strpos($arg, '=');
            $sets = $eq_pos !== false && $eq_pos != strlen($arg)-1;

            if ($sets) {
                $arg_value = substr($arg, $eq_pos+1);
                $arg = substr($arg, 0, $eq_pos);
            }

            if ($arg == str_replace(':', '', $arg_name) || $arg == str_replace(':', '', $arg_long_name)) {
                if (!$expects_value) {
                    $arguments[$arg_name] = true;
                } else {
                    
                    if ($sets) {
                        $arguments[$arg_name] = $arg_value;
                    }
                    else {
                        $i++;
                        $arguments[$arg_name] = $argv[$i];
                    }
                }
                break;
            }
        }
    }
}

function print_help($app_name) {
    echo "Usage: $app_name [-t|--test-arg] whattosetitto\n";
    echo "\t$app_name description and notes go here\n\n";

}

if (array_key_exists('-h', $arguments)) {
    print_help($app_name);
    exit;
}

$test_arg = $arguments['-t:'];

This method supports the following formats:

  • script_name -s "value"
  • script_name --full-name value
  • script_name --full-name=value
  • script_name -w
  • script_name --whatever
This allows the user to put things into the command line the same way they would for any standard unix/linux command.

Getting the values from the command line is done by just accessing an array using the switch's short-hand as the key. For Example:

//pull the value from an argument that has one;
// $# php ./args_test.php --argument="I just set this to anything I want."
$some_value = $arguments['-a:'];
echo $some_value; //outputs: "I just set this to anything I want."

//find out if someone turned on a non-settable argument
// $# php ./args_test.php -h
$is_switch_on = $arguments['-h'];
echo $is_switch_on === true; //outputs "1"
to comment, login using [ google, twitter, facebook ]
0 comments