Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Console
- {
- public function __construct($argv)
- {
- $this->config = array_merge($this->config, $this->configure($argv));
- }
- protected function configure($argv)
- {
- $config["exec"] = array_shift($argv);
- while (($key = array_shift($argv))) {
- switch ($key{0}) {
- case "-":
- switch ($key{1}) {
- case "-":
- if (($dlim = strpos($key, "="))) {
- $config[substr($key, 2, $dlim - 2)] = substr($key, $dlim + 1);
- } else
- $config[substr($key, 2)] = array_shift($argv);
- break;
- default:
- $config["flags"][substr($key, 1)] = true;
- }
- break;
- }
- }
- return $config;
- }
- protected function isFlagged($name)
- {
- if (!isset($this->flags[$name])) {
- throw new \InvalidArgumentException("invalid flag requested {$name}");
- }
- return $this->flags[$name];
- }
- protected function getConfig($name)
- {
- if (!isset($this->config[$name])) {
- throw new \InvalidArgumentException("invalid configuration requested {$name}");
- }
- return $this->config[$name];
- }
- public static function main($argv = array())
- {
- return new static($argv);
- }
- protected $config = array("exec" => null, "flags" => array("default" => false));
- }
- class My extends Console
- {
- public function __construct($argv)
- {
- parent::__construct($argv);
- try {
- $input = $this->getConfig("input");
- $output = $this->getConfig("output");
- /* and so on */
- }
- catch (\InvalidArgumentException $error) {
- printf("Command line configuration error:\n\t%s", (string) $error);
- }
- }
- }
- return My::main($argv);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement