Skip to content

Commit

Permalink
feat: add getOption() and improve hasOption() of Cli class (#146)
Browse files Browse the repository at this point in the history
* feat: improve `hasOption`
* feat: add `getOption`
* refactor: add type hints
* docs: add `getOption` description
* refactor: remove unused code
* feat: add backwards compatibility
* fix: unknown return type
The `mixed` pseudo type is only supported in PHP 8 or newer.
https://php.watch/versions/8.0/mixed-type
  • Loading branch information
grandeljay authored Nov 16, 2023
1 parent 26e7a8b commit 59a2f41
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/Classes/Cli/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,47 @@ public function getFilteredArgument(int $argumentIndex): string
return $filteredArguments[$argumentIndex] ?? '';
}

public function hasOption($option)
public function hasOption(string $option): bool
{
global $argv;
return in_array($option, $argv);

if (in_array($option, $argv)) {
return true;
};

foreach ($argv as $value) {
$optionParts = explode('=', $value);
$optionName = $optionParts[0] ?? $value;

if ($option === $optionName) {
return true;
}
}

return false;
}

/**
* Returns an option from the command line.
*
* @param string $option
*
* @return string
*/
public function getOption(string $option): string
{
global $argv;

foreach ($argv as $index => $value) {
$optionParts = explode('=', $value);
$optionName = $optionParts[0] ?? $value;
$optionValue = $optionParts[1] ?? '';

if ($option === $optionName) {
return $optionValue;
}
}

return '';
}
}

0 comments on commit 59a2f41

Please sign in to comment.