Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1383 - CLI project args #1384

Merged
merged 3 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,34 +204,39 @@ public function parseParameters(array $parameters = [], $possibleAlias = []): ar

$arguments = [];
foreach ($parameters as $parameter => $description) {
if (strpos($parameter, "=") !== false) {
$parameterParts = explode("=", $parameter);
if (count($parameterParts) != 2) {
if (strpos($parameter, '=') !== false) {
$parameterParts = explode('=', $parameter);
if (count($parameterParts) !== 2) {
throw new CommandsException("Invalid definition for the parameter '$parameter'");
}
if (strlen($parameterParts[0]) == "") {

if (strlen($parameterParts[0]) == 0) {
throw new CommandsException("Invalid definition for the parameter '$parameter'");
}

if (!in_array($parameterParts[1], ['s', 'i', 'l'])) {
throw new CommandsException("Incorrect data type on parameter '$parameter'");
}

$this->preparedArguments[$parameterParts[0]] = true;
$arguments[$parameterParts[0]] = [
'have-option' => true,
'option-required' => true,
'data-type' => $parameterParts[1]
'data-type' => $parameterParts[1],
];
} else {
if (!preg_match('/([a-zA-Z0-9]+)/', $parameter)) {
throw new CommandsException("Invalid parameter '$parameter'");
}

$this->preparedArguments[$parameter] = true;
$arguments[$parameter] = [
'have-option' => false,
'option-required' => false
];
continue;
}

if (!preg_match('/([a-zA-Z0-9]+)/', $parameter)) {
throw new CommandsException("Invalid parameter '$parameter'");
}

$this->preparedArguments[$parameter] = true;
$arguments[$parameter] = [
'have-option' => false,
'option-required' => false
];
}

$param = '';
Expand Down Expand Up @@ -267,26 +272,32 @@ public function parseParameters(array $parameters = [], $possibleAlias = []): ar
$param = '';
$paramName = '';
}
if ($arguments[$paramName]['have-option'] == false) {

if (!$arguments[$paramName]['have-option']) {
$receivedParams[$paramName] = true;
} elseif (isset($matches[4])) {
$receivedParams[$paramName] = $matches[4];
}
}
} else {
$param = $argv;
if ($paramName != '') {
if (!empty($arguments[$paramName]['have-option']) && $param == '') {
throw new CommandsException("The parameter '$paramName' requires an option");
}

$receivedParams[$paramName] = $param;
$param = '';
$paramName = '';
} else {
$receivedParams[$i - 1] = $param;
$param = '';
continue;
}

$param = $argv;
if ($paramName != '') {
/**
* @psalm-suppress InvalidArgument
*/
if (!empty($arguments[$paramName]['have-option']) && $param == '') {
throw new CommandsException("The parameter '$paramName' requires an option");
}

$receivedParams[$paramName] = $param;
$param = '';
$paramName = '';
} else {
$receivedParams[$i - 1] = $param;
$param = '';
}
}

Expand All @@ -299,6 +310,7 @@ public function parseParameters(array $parameters = [], $possibleAlias = []): ar
if (!empty($optionsToMerge)) {
$receivedParams = array_merge($optionsToMerge->toArray(), $receivedParams);
}

break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/console/GenerateProjectCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@
$I->seeFileFound(app_path($path3));
$I->seeFileFound(app_path($path3 . '/app/config/config.ini'));
$I->deleteDir(app_path($path3));

/**
* Case 4 - custom template engine
*/
$projectName4 = 'hello_world';
$path4 = $projectsFolder . '/' . $projectName4;

$I->dontSeeFileFound(app_path($path4));
$I->runShellCommand('phalcon project ' . $projectName4 . ' simple --template-engine=volt');
$I->seeFileFound(app_path($path4));
$I->seeFileFound(app_path($path4 . '/app/views/index.volt'));
$I->deleteDir(app_path($path4));