public static function parse($cmd_args, $stdin_stream = null, $stdout_stream = null) { if (!$stdin_stream) { $stdin_stream = fopen('php://stdin', 'r'); } if (!$stdout_stream) { $stdout_stream = fopen('php://stdout', 'w'); } array_shift($cmd_args); $actions = array(); $options = array(); foreach ($cmd_args as $arg) { if (substr($arg, 0, 1) === '-') { $arg = preg_replace('/^-*/', '', $arg); $arg_parts = explode("=", $arg); if (!isset($arg_parts[1])) { $arg_parts[1] = true; } $options[$arg_parts[0]] = $arg_parts[1]; } else { $actions[] = $arg; } } $first = array_shift($actions); if (!$first) { $first = 'Nth'; } $script_file = Framework::scriptLocate($first); if (!$script_file) { array_unshift($actions, $first); $first = 'Nth'; $script_file = Framework::scriptLocate($first); } if ($script_file) { require $script_file; $class = $first; $action = array_shift($actions); } else { throw new Exception('Script not found'); } $scope = new $first($options, $stdin_stream, $stdout_stream); while ($scope instanceof Script) { if ($scope->isValidMethod($action, $scope)) { $scope = $scope->{$action}($actions, $options); $action = array_shift($actions); } else { $action = 'help'; } } }