Match commandline arguments

<?php 
$args_raw = $_SERVER['argv'];
$args = array();
$args_num = array();
while (count($args_raw) > 0) {
    $a = array_shift($args_raw);
    $a = trim($a);
    if ($a{0} == '-') {
        $b = array_shift($args_raw);
        $b = trim($b);
        if ($b{0} == '-') {
            array_unshift($args_raw, $b);
            $b = '';
        }
        $a = trim($a, '-');
        $args[$a] = $b;
    } else {
        $args_num[] = $a;
    }
}
 
if ($args) {
    echo "Matched arguments:\n";
    print_r($args);
}
 
if ($args_num) {
    echo "Matched numeric arguments:\n";
    print_r($args_num);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.