From 3ff2d0daed95fec7fd781cb0953a82e5d147c486 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 10 Nov 2013 16:08:38 +0100 Subject: [PATCH] Add help cli command Closes #1561 --- .../install/files/lib/system/CLIWCF.class.php | 2 +- .../cli/command/CronjobCLICommand.class.php | 33 +++++--- .../cli/command/HelpCLICommand.class.php | 78 +++++++++++++++++++ .../command/IArgumentedCLICommand.class.php | 21 +++++ .../cli/command/PackageCLICommand.class.php | 25 +++--- .../cli/command/WorkerCLICommand.class.php | 49 ++++++++---- wcfsetup/install/lang/de.xml | 7 +- wcfsetup/install/lang/en.xml | 6 +- 8 files changed, 180 insertions(+), 41 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/cli/command/HelpCLICommand.class.php create mode 100644 wcfsetup/install/files/lib/system/cli/command/IArgumentedCLICommand.class.php diff --git a/wcfsetup/install/files/lib/system/CLIWCF.class.php b/wcfsetup/install/files/lib/system/CLIWCF.class.php index 5eb147b5f9..0b06c3d8e1 100644 --- a/wcfsetup/install/files/lib/system/CLIWCF.class.php +++ b/wcfsetup/install/files/lib/system/CLIWCF.class.php @@ -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); diff --git a/wcfsetup/install/files/lib/system/cli/command/CronjobCLICommand.class.php b/wcfsetup/install/files/lib/system/cli/command/CronjobCLICommand.class.php index 82ac169a67..11d57f628e 100644 --- a/wcfsetup/install/files/lib/system/cli/command/CronjobCLICommand.class.php +++ b/wcfsetup/install/files/lib/system/cli/command/CronjobCLICommand.class.php @@ -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 index 0000000000..f947edb24c --- /dev/null +++ b/wcfsetup/install/files/lib/system/cli/command/HelpCLICommand.class.php @@ -0,0 +1,78 @@ + + * @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 ] ', $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 index 0000000000..07ecdd08c1 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cli/command/IArgumentedCLICommand.class.php @@ -0,0 +1,21 @@ + + * @package com.woltlab.wcf + * @subpackage system.cli.command + * @category Community Framework + */ +interface IArgumentedCLICommand extends ICLICommand { + /** + * Returns the usage text. + * + * @return string + */ + public function getUsage(); +} diff --git a/wcfsetup/install/files/lib/system/cli/command/PackageCLICommand.class.php b/wcfsetup/install/files/lib/system/cli/command/PackageCLICommand.class.php index 265d2100bb..f3f5b5b0a2 100644 --- a/wcfsetup/install/files/lib/system/cli/command/PackageCLICommand.class.php +++ b/wcfsetup/install/files/lib/system/cli/command/PackageCLICommand.class.php @@ -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 ] ', $usage); + public function getUsage() { + return str_replace($_SERVER['argv'][0].' [ options ]', 'package [ options ] ', $this->argv->getUsageMessage()); } /** diff --git a/wcfsetup/install/files/lib/system/cli/command/WorkerCLICommand.class.php b/wcfsetup/install/files/lib/system/cli/command/WorkerCLICommand.class.php index 562e15c256..33c985f0f1 100644 --- a/wcfsetup/install/files/lib/system/cli/command/WorkerCLICommand.class.php +++ b/wcfsetup/install/files/lib/system/cli/command/WorkerCLICommand.class.php @@ -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 ] ', $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 ] ', $this->argv->getUsageMessage()); + } + /** * @see \wcf\system\cli\command\ICLICommand::canAccess() */ diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 5c7956e77f..0df8f4f056 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -1442,8 +1442,13 @@ Fehler sind beispielsweise: - Befehle, welche ihre Aktion nicht erfolgreich ausführen konnten - Systemfehler]]> + + + + + - + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index c687ffa47f..460a3e723b 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -1442,8 +1442,12 @@ Errors are: - Core errors]]> + + + + - + -- 2.20.1