Skip to content

Commit

Permalink
Implement get functions, closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Feb 16, 2019
1 parent 27d62f0 commit 26e543c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 105 deletions.
124 changes: 22 additions & 102 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,46 @@ 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 = "";

$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();

Expand All @@ -82,7 +70,7 @@ public function getUsage():string {
}
}

foreach($this->optionalParameterList as $parameter) {
foreach($this->getOptionalParameterList() as $parameter) {
$message .= " [--";
$message .= $parameter->getLongOption();

Expand All @@ -103,7 +91,7 @@ public function getUsage():string {

public function checkArguments(ArgumentList $argumentList):void {
$numRequiredNamedParameters = count(
$this->requiredNamedParameterList
$this->getRequiredNamedParameterList()
);

$passedNamedArguments = 0;
Expand All @@ -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
Expand All @@ -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();
Expand Down
38 changes: 35 additions & 3 deletions src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 26e543c

Please sign in to comment.