From 26e543c37b7953959d8cd73237796c7a5d9f2873 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Sat, 16 Feb 2019 17:27:34 +0000 Subject: [PATCH] Implement get functions, closes #22 --- src/Command/Command.php | 124 +++++++----------------------------- src/Command/HelpCommand.php | 38 ++++++++++- 2 files changed, 57 insertions(+), 105 deletions(-) diff --git a/src/Command/Command.php b/src/Command/Command.php index 1468362..938fd27 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -17,39 +17,27 @@ abstract class Command { /** @var Stream */ protected $output; - protected $name; - protected $description = ""; - /** @var NamedParameter[] */ - protected $optionalNamedParameterList = []; - /** @var NamedParameter[] */ - protected $requiredNamedParameterList = []; - /** @var Parameter[] */ - protected $optionalParameterList = []; - /** @var Parameter[] */ - protected $requiredParameterList = []; - public function setOutput(Stream $output = null) { $this->output = $output; } - abstract public function run(ArgumentValueList $arguments = null):void; + abstract public function getName():string; - public function getName():string { - return $this->name; - } + abstract public function getDescription():string; - protected function setName(string $name):void { - $this->name = $name; - } + /** @return NamedParameter[] */ + abstract public function getRequiredNamedParameterList():array; - public function getDescription():string { - return $this->description; - } + /** @return NamedParameter[] */ + abstract public function getOptionalNamedParameterList():array; - protected function setDescription(string $description):void { - $this->description = $description; - } + /** @return Parameter[] */ + abstract public function getRequiredParameterList():array; + /** @return Parameter[] */ + abstract public function getOptionalParameterList():array; + + abstract public function run(ArgumentValueList $arguments = null):void; public function getUsage():string { $message = ""; @@ -57,18 +45,18 @@ public function getUsage():string { $message .= "Usage: "; $message .= $this->getName(); - foreach($this->requiredNamedParameterList as $parameter) { + foreach($this->getRequiredNamedParameterList() as $parameter) { $message .= " "; $message .= $parameter->getOptionName(); } - foreach($this->optionalNamedParameterList as $parameter) { + foreach($this->getOptionalNamedParameterList() as $parameter) { $message .= " ["; $message .= $parameter->getOptionName(); $message .= "]"; } - foreach($this->requiredParameterList as $parameter) { + foreach($this->getRequiredParameterList() as $parameter) { $message .= " --"; $message .= $parameter->getLongOption(); @@ -82,7 +70,7 @@ public function getUsage():string { } } - foreach($this->optionalParameterList as $parameter) { + foreach($this->getOptionalParameterList() as $parameter) { $message .= " [--"; $message .= $parameter->getLongOption(); @@ -103,7 +91,7 @@ public function getUsage():string { public function checkArguments(ArgumentList $argumentList):void { $numRequiredNamedParameters = count( - $this->requiredNamedParameterList + $this->getRequiredNamedParameterList() ); $passedNamedArguments = 0; @@ -120,7 +108,7 @@ public function checkArguments(ArgumentList $argumentList):void { ); } - foreach($this->requiredParameterList as $parameter) { + foreach($this->getRequiredParameterList() as $parameter) { if(!$argumentList->contains($parameter)) { throw new MissingRequiredParameterException( $parameter @@ -140,89 +128,21 @@ public function checkArguments(ArgumentList $argumentList):void { } } - /** - * @return NamedParameter[] - */ - public function getRequiredNamedParameterList():array { - return $this->requiredNamedParameterList; - } - - /** - * @return NamedParameter[] - */ - public function getOptionalNamedParameterList():array { - return $this->optionalNamedParameterList; - } - - protected function setRequiredNamedParameter(string $name):void { - $this->requiredNamedParameterList []= new NamedParameter( - $name - ); - } - - /** - * @return Parameter[] - */ - public function getOptionalParameterList():array { - return $this->optionalParameterList; - } - - protected function setOptionalNamedParameter(string $name):void { - $this->optionalNamedParameterList []= new NamedParameter( - $name - ); - } - - protected function setOptionalParameter( - bool $requireValue, - string $longOption, - string $shortOption = null, - string $example = null - ):void { - $this->optionalParameterList []= new Parameter( - $requireValue, - $longOption, - $shortOption, - $example - ); - } - - /** - * @return Parameter[] - */ - public function getRequiredParameterList():array { - return $this->requiredParameterList; - } - - protected function setRequiredParameter( - bool $requireValue, - string $longOption, - string $shortOption = null, - string $example = null - ):void { - $this->requiredParameterList []= new Parameter( - $requireValue, - $longOption, - $shortOption, - $example - ); - } - public function getArgumentValueList( ArgumentList $arguments ):ArgumentValueList { $namedParameterIndex = 0; /** @var NamedParameter[] */ $namedParameterList = array_merge( - $this->requiredNamedParameterList, - $this->optionalNamedParameterList + $this->getRequiredNamedParameterList(), + $this->getOptionalNamedParameterList() ); $parameterIndex = 0; /** @var Parameter[] $parameterList */ $parameterList = array_merge( - $this->requiredParameterList, - $this->optionalParameterList + $this->getRequiredParameterList(), + $this->getOptionalParameterList() ); $argumentValueList = new ArgumentValueList(); diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index 139f311..57a33a8 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -2,6 +2,8 @@ namespace Gt\Cli\Command; use Gt\Cli\Argument\ArgumentValueList; +use Gt\Cli\Parameter\NamedParameter; +use Gt\Cli\Parameter\Parameter; class HelpCommand extends Command { protected $applicationName; @@ -15,14 +17,41 @@ public function __construct( string $applicationName, array $applicationCommandList = [] ) { - $this->setName("help"); - $this->setDescription("Display information about available commands"); - $this->applicationName = $applicationName; $this->applicationCommandList = $applicationCommandList; $this->applicationCommandList []= $this; } + public function getName():string { + return "help"; + } + + public function getDescription():string { + return "Display information about available commands"; + } + + /** @return NamedParameter[] */ + public function getRequiredNamedParameterList():array { + return []; + } + + /** @return NamedParameter[] */ + public function getOptionalNamedParameterList():array { + return [ + new NamedParameter("command"), + ]; + } + + /** @return Parameter[] */ + public function getRequiredParameterList():array { + return []; + } + + /** @return Parameter[] */ + public function getOptionalParameterList():array { + return []; + } + public function run(ArgumentValueList $arguments = null): void { $this->writeLine($this->applicationName); $this->writeLine(); @@ -51,5 +80,8 @@ public function run(ArgumentValueList $arguments = null): void { . $command->getDescription() ); } + + $this->writeLine(); + $this->writeLine("Type gt help COMMAND to get help for that command"); } } \ No newline at end of file