Skip to content

Commit

Permalink
Merge pull request #97 from adhocore/95-help-args-sort
Browse files Browse the repository at this point in the history
fix(helper): arguments help output must not be sorted #95
  • Loading branch information
adhocore authored Jan 22, 2024
2 parents c76e28e + 0709073 commit 3419131
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- php: '8.0'
- php: '8.1'
- php: '8.2'
- php: '8.3'
fail-fast: true

runs-on: ubuntu-20.04
Expand Down
9 changes: 7 additions & 2 deletions src/Helper/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected function showHelp(string $for, array $items, string $header = '', stri
$group = $lastGroup = null;

$withDefault = $for === 'Options' || $for === 'Arguments';
foreach ($this->sortItems($items, $padLen) as $item) {
foreach ($this->sortItems($items, $padLen, $for) as $item) {
$name = $this->getName($item);
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
$this->writer->boldYellow($group ?: '*', true);
Expand Down Expand Up @@ -271,13 +271,18 @@ public function showCommandNotFound(string $attempted, array $available): self
*
* @param Parameter[]|Command[] $items
* @param int $max
* @param string $for
*
* @return array
*/
protected function sortItems(array $items, &$max = 0): array
protected function sortItems(array $items, &$max = 0, string $for = ''): array
{
$max = max(array_map(fn ($item) => strlen($this->getName($item)), $items));

if ($for === 'Arguments') { // Arguments are positional so must not be sorted
return $items;
}

uasort($items, static function ($a, $b) {
$aName = $a instanceof Groupable ? $a->group() . $a->name() : $a->name();
$bName = $b instanceof Groupable ? $b->group() . $b->name() : $b->name();
Expand Down
4 changes: 2 additions & 2 deletions src/Output/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ protected function getProgressBarStr(int $current, string $label): string

if ($label) {
$label = $this->labelFormatted($label);
// If this line doesn't have a label, but we've had one before,
// then ensure the label line is cleared
// If this line doesn't have a label, but we've had one before,
// then ensure the label line is cleared
} elseif ($this->hasLabelLine) {
$label = $this->labelFormatted('');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Helper/OutputHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function test_show_arguments()
'Arg Header',
'',
'Arguments:',
' [config] [default: "defaultConfig"]',
' <path> The path',
' [config] [default: "defaultConfig"]',
'',
'Arg Footer',
], $this->output());
Expand Down

0 comments on commit 3419131

Please sign in to comment.