Add events and catch RuntimeException on malformed options
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 9 Nov 2012 22:23:44 +0000 (23:23 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 9 Nov 2012 22:23:44 +0000 (23:23 +0100)
wcfsetup/install/files/lib/system/CLIWCF.class.php

index a5bc4b9ddbfd8eacca0ebed2646c4a75a8ed0900..2d75d5a6cec536a1e2e91d2d15b9f6a20794428e 100644 (file)
@@ -5,10 +5,12 @@ use phpline\TerminalFactory;
 use wcf\system\cli\command\CommandHandler;
 use wcf\system\cli\command\CommandNameCompleter;
 use wcf\system\cli\DatabaseCommandHistory;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\UserInputException;
 use wcf\system\user\authentication\UserAuthenticationFactory;
 use wcf\util\StringUtil;
+use Zend\Console\Exception\RuntimeException as ArgvException;
 use Zend\Console\Getopt as ArgvParser;
 use Zend\Loader\StandardAutoloader as ZendLoader;
 
@@ -29,6 +31,12 @@ class CLIWCF extends WCF {
         */
        protected static $consoleReader = null;
        
+       /**
+        * instance of ArgvParser
+        * @var Zend\Console\Getopt
+        */
+       protected static $argvParser = null;
+       
        /**
         * Calls all init functions of the WCF class.
         */
@@ -50,19 +58,39 @@ class CLIWCF extends WCF {
         * Initializes parsing of command line options.
         */
        protected function initArgv() {
-               $opts = new ArgvParser(array(
+               self::$argvParser = new ArgvParser(array(
                        'v' => 'Verbose: Show more output',
                        'q' => 'Quiet: Show less output',
                        'h|help' => 'Show this help'
                ));
-               $opts->setOptions(array(ArgvParser::CONFIG_CUMULATIVE_FLAGS => true));
-               $opts->parse();
-               if ($opts->getOption('help')) {
-                       echo $opts->getUsageMessage();
+               self::getArgvParser()->setOptions(array(ArgvParser::CONFIG_CUMULATIVE_FLAGS => true));
+               
+               EventHandler::getInstance()->fireAction($this, 'beforeArgumentParsing');
+               try {
+                       self::getArgvParser()->parse();
+               }
+               catch (ArgvException $e) {
+                       echo $e->getMessage()."\n";
+                       echo $e->getUsageMessage();
+                       exit;
+               }
+               EventHandler::getInstance()->fireAction($this, 'afterArgumentParsing');
+               
+               if (self::getArgvParser()->getOption('help')) {
+                       echo self::getArgvParser()->getUsageMessage();
                        exit;
                }
        }
        
+       /**
+        * Returns the argv parser.
+        * 
+        * @return Zend\Console\Getopt
+        */
+       public static function getArgvParser() {
+               return self::$argvParser;
+       }
+       
        /**
         * Initializes PHPLine.
         */