lithium\console\Router

class

The Router class uses an instance of lithium\console\Request, which represents an incoming command-line invocation, to parse the correct command, and sub-command(s) and parameters, which are used by lithium\console\Dispatcher to load and execute the proper Command class.

Source

class Router extends \lithium\core\Object {

	/**
	 * Parse incoming request from console. Short and long (GNU-style) options
	 * in the form of `-f`, `--foo`, `--foo-bar` and `--foo=bar` are parsed.
	 * XF68-style long options (i.e. `-foo`) are not supported but support
	 * can be added by extending this class.
	 *
	 * If passing through `--foo-bar` this previously (pre 1.1) resulted in
	 * the option `foo-bar` being available. This has been deprecated in favor
	 * of `fooBar` being created.
	 *
	 * @param \lithium\console\Request $request
	 * @return array $params
	 */
	public static function parse($request = null) {
		$defaults = ['command' => null, 'action' => 'run', 'args' => []];
		$params = $request ? (array) $request->params + $defaults : $defaults;

		if (!empty($request->argv)) {
			$args = $request->argv;

			while ($args) {
				$arg = array_shift($args);
				if (preg_match('/^-(?P<key>[a-zA-Z0-9])$/i', $arg, $match)) {
					$key = Inflector::camelize($match['key'], false);
					$params[$key] = $params[$match['key']] = true;
					continue;
				}
				if (preg_match('/^--(?P<key>[a-z0-9-]+)(?:=(?P<val>.+))?$/i', $arg, $match)) {
					$key = Inflector::camelize($match['key'], false);
					$params[$key] = $params[$match['key']] = !isset($match['val']) ? true : $match['val'];
					continue;
				}
				$params['args'][] = $arg;
			}
		}
		foreach (['command', 'action'] as $param) {
			if (!empty($params['args'])) {
				$params[$param] = array_shift($params['args']);
			}
		}
		return $params;
	}
}