Skip to content

Commit

Permalink
fix: short option check and sort options map
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 14, 2022
1 parent 190cf6f commit 62bb1f2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/Contract/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public function hasShortOpts(): bool;
* @param string $type The argument data type. default is: string. {@see FlagType}
* @param bool $required
* @param mixed|null $default
* @param array $moreInfo
*
* @psalm-param array{alias: string, helpType: string} $moreInfo
* @param array{aliases: array, helpType: string} $moreInfo
*
* @return self
*/
Expand Down
2 changes: 1 addition & 1 deletion src/FlagUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function alignOptions(array $options): array
}

// check has short option. e.g '-h, --help'
$nameString = implode('|', array_keys($options));
$nameString = '|' . implode('|', array_keys($options));
if (preg_match('/\|-\w/', $nameString) !== 1) {
return $options;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function implode;
use function is_array;
use function is_string;
use function ksort;
use function sprintf;
use function str_split;
use function strlen;
Expand Down Expand Up @@ -746,9 +747,7 @@ protected function resetArguments(): void
* @param string $type The argument data type. default is: string. {@see FlagType}
* @param bool $required
* @param mixed|null $default
* @param array $moreInfo
*
* @psalm-param array{aliases: array, helpType: string} $moreInfo
* @param array{aliases: array, helpType: string} $moreInfo
*
* @return static
*/
Expand Down Expand Up @@ -1023,6 +1022,7 @@ public function getOptsHelpLines(): array
$helpData[$helpName] = $fmtDesc;
}

ksort($helpData);
return $helpData;
}

Expand Down
11 changes: 7 additions & 4 deletions src/SFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function implode;
use function is_callable;
use function is_string;
use function ksort;
use function next;
use function sprintf;
use function str_split;
Expand Down Expand Up @@ -149,9 +150,7 @@ public function isNotEmpty(): bool
* @param string $type The argument data type. default is: string. {@see FlagType}
* @param bool $required
* @param mixed|null $default
* @param array $moreInfo
*
* @psalm-param array{alias: string, helpType: string} $moreInfo
* @param array{aliases: array<string>, helpType: string} $moreInfo
*
* @return SFlags
*/
Expand All @@ -177,6 +176,9 @@ public function addOpt(
if (isset($moreInfo['helpType'])) {
$define['helpType'] = $moreInfo['helpType'];
}
if (isset($moreInfo['aliases'])) {
$define['aliases'] = $moreInfo['aliases'];
}

$this->addOptDefine($define);
return $this;
Expand Down Expand Up @@ -706,7 +708,7 @@ protected function addOptDefine(array $define): void
$this->opts[$name] = FlagType::fmtBasicTypeValue($type, $envVal);
}

// has shorts
// has aliases
if ($define['aliases']) {
foreach ($define['aliases'] as $alias) {
$this->setAlias($name, $alias, true);
Expand Down Expand Up @@ -1057,6 +1059,7 @@ public function getOptsHelpLines(): array
$helpLines[$helpName] = $fmtDesc;
}

ksort($helpLines);
return $helpLines;
}

Expand Down

0 comments on commit 62bb1f2

Please sign in to comment.