Add help cli command
authorTim Düsterhus <duesterhus@woltlab.com>
Sun, 10 Nov 2013 15:08:38 +0000 (16:08 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 10 Nov 2013 15:08:38 +0000 (16:08 +0100)
Closes #1561

wcfsetup/install/files/lib/system/CLIWCF.class.php
wcfsetup/install/files/lib/system/cli/command/CronjobCLICommand.class.php
wcfsetup/install/files/lib/system/cli/command/HelpCLICommand.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/cli/command/IArgumentedCLICommand.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/cli/command/PackageCLICommand.class.php
wcfsetup/install/files/lib/system/cli/command/WorkerCLICommand.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 5eb147b5f9658f1bffc1305e75b37b5c0e06b78c..0b06c3d8e1c3e20e3f499bb8d65617f7ac6323d9 100644 (file)
@@ -301,7 +301,7 @@ class CLIWCF extends WCF {
                        catch (ArgvException $e) {
                                // show error message and usage
                                if ($e->getMessage()) echo $e->getMessage().PHP_EOL;
-                               echo str_replace($_SERVER['argv'][0], CLICommandHandler::getCommandName($line), $e->getUsageMessage());
+                               echo $e->getUsageMessage();
                                
                                if (self::getArgvParser()->exitOnFail) {
                                        exit(1);
index 82ac169a6755f8163482b8ce79a7dbd1f8dd51dc..11d57f628e378c16bd0b4d87d8ebc7c7ffa478c6 100644 (file)
@@ -14,31 +14,40 @@ use Zend\Console\Getopt as ArgvParser;
  * @subpackage system.cli.command
  * @category   Community Framework
  */
-class CronjobCLICommand implements ICLICommand {
+class CronjobCLICommand implements IArgumentedCLICommand {
+       /**
+        * arguments parser
+        * @var \Zend\Console\Getopt
+        */
+       protected $argv = null;
+       
+       /**
+        * Initializes the argument parser.
+        */
+       public function __construct() {
+               $this->argv = new ArgvParser(array());
+       }
+       
        /**
         * @see \wcf\system\cli\command\ICLICommand::execute()
         */
        public function execute(array $parameters) {
-               $argv = new ArgvParser(array());
-               $argv->setArguments($parameters);
-               $argv->parse();
+               $this->argv->setArguments($parameters);
+               $this->argv->parse();
                
-               $args = $argv->getRemainingArgs();
+               $args = $this->argv->getRemainingArgs();
                if (count($args) != 1 || $args[0] != 'execute') {
-                       throw new ArgvException('', $this->fixUsage($argv->getUsageMessage()));
+                       throw new ArgvException('', $this->getUsage());
                }
                
                CronjobScheduler::getInstance()->executeCronjobs();
        }
        
        /**
-        * Returns fixed usage message of ArgvParser.
-        * 
-        * @param       string          $usage
-        * @return      string
+        * @see \wcf\system\cli\command\ICLICommand::getUsage()
         */
-       public function fixUsage($usage) {
-               return str_replace($_SERVER['argv'][0].' [ options ]', $_SERVER['argv'][0].' [ options ] execute', $usage);
+       public function getUsage() {
+               return str_replace($_SERVER['argv'][0].' [ options ]', 'cronjob [ options ] execute', $this->argv->getUsageMessage());
        }
        
        /**
diff --git a/wcfsetup/install/files/lib/system/cli/command/HelpCLICommand.class.php b/wcfsetup/install/files/lib/system/cli/command/HelpCLICommand.class.php
new file mode 100644 (file)
index 0000000..f947edb
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+namespace wcf\system\cli\command;
+use wcf\system\CLIWCF;
+use wcf\util\CLIUtil;
+use wcf\util\DirectoryUtil;
+use wcf\util\StringUtil;
+use Zend\Console\Exception\RuntimeException as ArgvException;
+use Zend\Console\Getopt as ArgvParser;
+
+/**
+ * Shows command usage.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.cli.command
+ * @category   Community Framework
+ */
+class HelpCLICommand implements IArgumentedCLICommand {
+       /**
+        * arguments parser
+        * @var \Zend\Console\Getopt
+        */
+       protected $argv = null;
+               
+       /**
+        * Initializes the argument parser.
+        */
+       public function __construct() {
+               $this->argv = new ArgvParser(array());
+               $this->argv->setOptions(array(
+                       ArgvParser::CONFIG_FREEFORM_FLAGS => true,
+                       ArgvParser::CONFIG_PARSEALL => false,
+                       ArgvParser::CONFIG_CUMULATIVE_PARAMETERS => true
+               ));
+       }
+       
+       /**
+        * @see \wcf\system\cli\command\ICLICommand::execute()
+        */
+       public function execute(array $parameters) {
+               $this->argv->setArguments($parameters);
+               $this->argv->parse();
+               
+               $args = $this->argv->getRemainingArgs();
+               // validate parameters
+               if (count($args) != 1) {
+                       throw new ArgvException('', $this->getUsage());
+               }
+               $commands = CLICommandHandler::getCommands();
+               
+               if (!isset($commands[$args[0]])) {
+                       throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.cli.error.command.notFound', array('command' => $args[0])), $this->getUsage());
+               }
+               
+               $command = $commands[$args[0]];
+               if (!($command instanceof IArgumentedCLICommand)) {
+                       throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.cli.error.help.noArguments', array('command' => $args[0])), $this->getUsage());
+               }
+               
+               CLIWCF::getReader()->println($command->getUsage());
+       }
+       
+       /**
+        * @see \wcf\system\cli\command\ICLICommand::getUsage()
+        */
+       public function getUsage() {
+               return str_replace($_SERVER['argv'][0].' [ options ]', 'help [ options ] <command>', $this->argv->getUsageMessage());
+       }
+       
+       /**
+        * @see \wcf\system\cli\command\ICLICommand::canAccess()
+        */
+       public function canAccess() {
+               return true;
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/cli/command/IArgumentedCLICommand.class.php b/wcfsetup/install/files/lib/system/cli/command/IArgumentedCLICommand.class.php
new file mode 100644 (file)
index 0000000..07ecdd0
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+namespace wcf\system\cli\command;
+
+/**
+ * Represents a command with arguments.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.cli.command
+ * @category   Community Framework
+ */
+interface IArgumentedCLICommand extends ICLICommand {
+       /**
+        * Returns the usage text.
+        * 
+        * @return      string
+        */
+       public function getUsage();
+}
index 265d2100bbcf672191cfc49d280bb294d3f03a58..f3f5b5b0a297c8c555685ed723ab91b14cae5cbe 100644 (file)
@@ -29,18 +29,24 @@ use Zend\ProgressBar\ProgressBar;
  * @subpackage system.cli.command
  * @category   Community Framework
  */
-class PackageCLICommand implements ICLICommand {
+class PackageCLICommand implements IArgumentedCLICommand {
        /**
         * arguments parser
         * @var \Zend\Console\Getopt
         */
-       private $argv = null;
+       protected $argv = null;
+       
+       /**
+        * Initializes the argument parser.
+        */
+       public function __construct() {
+               $this->argv = new ArgvParser(array());
+       }
        
        /**
         * @see \wcf\system\cli\command\ICLICommand::execute()
         */
        public function execute(array $parameters) {
-               $this->argv = new ArgvParser(array());
                $this->argv->setArguments($parameters);
                $this->argv->parse();
                
@@ -447,21 +453,18 @@ class PackageCLICommand implements ICLICommand {
                Log::error('package.'.$name.':'.JSON::encode($parameters));
                
                if ($parameters) {
-                       throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.acp.package.error.'.$name, $parameters), $this->fixUsage($this->argv->getUsageMessage()));
+                       throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.acp.package.error.'.$name, $parameters), $this->getUsage());
                }
                else {
-                       throw new ArgvException(CLIWCF::getLanguage()->get('wcf.acp.package.error.'.$name), $this->fixUsage($this->argv->getUsageMessage()));
+                       throw new ArgvException(CLIWCF::getLanguage()->get('wcf.acp.package.error.'.$name), $this->argv->getUsageMessage());
                }
        }
        
        /**
-        * Returns fixed usage message of ArgvParser.
-        * 
-        * @param       string          $usage
-        * @return      string
+        * @see \wcf\system\cli\command\ICLICommand::getUsage()
         */
-       public function fixUsage($usage) {
-               return str_replace($_SERVER['argv'][0].' [ options ]', $_SERVER['argv'][0].' [ options ] <install|uninstall> <package>', $usage);
+       public function getUsage() {
+               return str_replace($_SERVER['argv'][0].' [ options ]', 'package [ options ] <install|uninstall> <package>', $this->argv->getUsageMessage());
        }
        
        /**
index 562e15c256ea728427c8219efd9cbb911c86c879..33c985f0f1bf74b9d4327116f6023dcf9438beb9 100644 (file)
@@ -22,32 +22,44 @@ use Zend\ProgressBar\ProgressBar;
  * @subpackage system.cli.command
  * @category   Community Framework
  */
-class WorkerCLICommand implements ICLICommand {
+class WorkerCLICommand implements IArgumentedCLICommand {
        /**
-        * @see \wcf\system\cli\command\ICLICommand::execute()
+        * arguments parser
+        * @var \Zend\Console\Getopt
         */
-       public function execute(array $parameters) {
-               $argv = new ArgvParser(array(
-                       'l|list' => 'Lists available workers',
-                       'setParameter=s' => 'Sets a parameter given to the worker'
+       protected $argv = null;
+       
+       /**
+        * Initializes the argument parser.
+        */
+       public function __construct() {
+               $this->argv = new ArgvParser(array(
+                       'l|list' => CLIWCF::getLanguage()->get('wcf.cli.worker.list'),
+                       'setParameter=s' => CLIWCF::getLanguage()->get('wcf.cli.worker.setParameter')
                ));
-               $argv->setOptions(array(
+               $this->argv->setOptions(array(
                        ArgvParser::CONFIG_FREEFORM_FLAGS => true,
                        ArgvParser::CONFIG_PARSEALL => false,
                        ArgvParser::CONFIG_CUMULATIVE_PARAMETERS => true
                ));
-               $argv->setArguments($parameters);
-               $argv->parse();
+       }
+       
+       /**
+        * @see \wcf\system\cli\command\ICLICommand::execute()
+        */
+       public function execute(array $parameters) {
+               $this->argv->setArguments($parameters);
+               $this->argv->parse();
                
-               if ($argv->list) {
+               if ($this->argv->list) {
                        CLIWCF::getReader()->println(CLIUtil::generateTable($this->generateList()));
                        return;
                }
                
-               $args = $argv->getRemainingArgs();
+               $args = $this->argv->getRemainingArgs();
                // validate parameters
                if (count($args) != 1) {
-                       throw new ArgvException('', str_replace($_SERVER['argv'][0].' [ options ]', $_SERVER['argv'][0].' [ options ] <worker>', $argv->getUsageMessage()));
+                       throw new ArgvException('', $this->getUsage());
                }
                
                $class = $args[0];
@@ -71,14 +83,14 @@ class WorkerCLICommand implements ICLICommand {
                        }
                }
                if ($invalid) {
-                       throw new ArgvException("Invalid worker '".$class."' given", $argv->getUsageMessage());
+                       throw new ArgvException("Invalid worker '".$class."' given", $this->getUsage());
                }
                
                // parse parameters
-               $options = $argv->getOptions();
+               $options = $this->argv->getOptions();
                $parameters = array();
                foreach ($options as $option) {
-                       $value = $argv->getOption($option);
+                       $value = $this->argv->getOption($option);
                        if ($option === 'setParameter') {
                                if (!is_array($value)) {
                                        $value = array($value);
@@ -165,6 +177,13 @@ class WorkerCLICommand implements ICLICommand {
                return $table;
        }
        
+       /**
+        * @see \wcf\system\cli\command\ICLICommand::getUsage()
+        */
+       public function getUsage() {
+               return str_replace($_SERVER['argv'][0].' [ options ]', 'worker [ options ] <worker>', $this->argv->getUsageMessage());
+       }
+       
        /**
         * @see \wcf\system\cli\command\ICLICommand::canAccess()
         */
index 5c7956e77f0bdc8af2d22e15731fd08f4a7bb32a..0df8f4f0561a665008c5ec8183ba63d053276515 100644 (file)
@@ -1442,8 +1442,13 @@ Fehler sind beispielsweise:
 - Befehle, welche ihre Aktion nicht erfolgreich ausführen konnten
 - Systemfehler]]></item>
                <item name="wcf.cli.help.packageID"><![CDATA[Die Anwendung mit der angegebenen Paket-ID wird als Standard für diese Sitzung verwendet.]]></item>
+               
+               <item name="wcf.cli.worker.setParameter"><![CDATA[Übergibt einen Parameter an den Worker. Bsp.: --setParameter param=wert]]></item>
+               <item name="wcf.cli.worker.list"><![CDATA[Listet alle Worker auf.]]></item>
+               
+               <item name="wcf.cli.error.help.noArguments"><![CDATA[Der Befehl "{$command}" unterstützt keine Parameter.]]></item>
                <item name="wcf.cli.error.language.notFound"><![CDATA[Die Sprache mit dem Sprachcode "{$languageCode}" konnte nicht gefunden werden.]]></item>
-               <item name="wcf.cli.error.command.notFound"><![CDATA[Der Befehl "{$command}" konnte nicht gefunden werden.]]></item>
+               <item name="wcf.cli.error.command.notFound"><![CDATA[Der Befehl "{$command}" konnte nicht gefunden werden. Benutze "commands", um verfügbare Befehle aufzulisten.]]></item>
        </category>
        
        <category name="wcf.clipboard">
index c687ffa47fc77f37f13b4dbdbc720bc52dbf4b8f..460a3e723bc9efce2fe208aab03a8d2191d0092f 100644 (file)
@@ -1442,8 +1442,12 @@ Errors are:
 - Core errors]]></item>
                <item name="wcf.cli.help.packageID"><![CDATA[The given package id will be used as a default for this session.]]></item>
                
+               <item name="wcf.cli.worker.setParameter"><![CDATA[Sets a worker parameter e.g.: --setParameter param=value]]></item>
+               <item name="wcf.cli.worker.list"><![CDATA[Lists all workers.]]></item>
+               
+               <item name="wcf.cli.error.help.noArguments"><![CDATA[The command "{$command}" does not support any parameters.]]></item>
                <item name="wcf.cli.error.language.notFound"><![CDATA[The language with the language code "{$languageCode}" could not be found.]]></item>
-               <item name="wcf.cli.error.command.notFound"><![CDATA[The command "{$command}" could not be found.]]></item>
+               <item name="wcf.cli.error.command.notFound"><![CDATA[The command "{$command}" could not be found. Type in "commands" to list available commands.]]></item>
        </category>
        
        <category name="wcf.clipboard">