<?php
// cd console
// php cmd.php build_lan --to=ms-my
require_once("index.php");
$args = parseArgs($_SERVER["argv"]);
$par = $args[0]?:false;
if ($par) {
    @include_once $par.".php";
    if(class_exists($par)){
        $application = new $par($registry);
        $application->run($args);
    }else{
        print "不可识别的参数\r\n";
    }
}else{
    print "缺少必要参数\r\n";
}

function getArg($par,$args)
{
    if ($args && isset($args[$par])) {
        return $args[$par];
    } else {
        return null;
    }
}

function parseArgs($argv)
{
    array_shift($argv);
    $out = array();
    foreach ($argv as $arg) {
        if (substr($arg, 0, 2) == '--') {
            $eqPos = strpos($arg, '=');
            if ($eqPos === false) {
                $key = substr($arg, 2);
                $out[$key] = isset($out[$key]) ? $out[$key] : true;
            } else {
                $key = substr($arg, 2, $eqPos - 2);
                $out[$key] = substr($arg, $eqPos + 1);
            }
        } else if (substr($arg, 0, 1) == '-') {
            if (substr($arg, 2, 1) == '=') {
                $key = substr($arg, 1, 1);
                $out[$key] = substr($arg, 3);
            } else {
                $chars = str_split(substr($arg, 1));
                foreach ($chars as $char) {
                    $key = $char;
                    $out[$key] = isset($out[$key]) ? $out[$key] : true;
                }
            }
        } else {
            $out[] = $arg;
        }
    }
    return $out;
}
