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

fix(helper): arguments help output must not be sorted #95 #97

Merged
merged 5 commits into from
Jan 22, 2024
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
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
Loading