Skip to content

Commit ea39bf6

Browse files
committed
Show all subcommands in usages from the top to a current branch.
1 parent c39be44 commit ea39bf6

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

docs/changelog.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ This change log references the repository changes and releases, which respect [s
1313
in your production classes under 'dev' environment (when you call `composer install` without `--no-dev` option),
1414
you will get "Class 'XXX' not found in ..." error.
1515
Previously there was no error, until you install composer packages with `--no-dev` flag.
16-
1. Fixed scripts main description block - stopped counting symbols in space-only lines.
17-
Previously it caused too much padding for descriptions with too short space-only lines.
16+
1. `HelpGenerator`:
17+
1. Fixed scripts main description block - stopped counting symbols in space-only lines.
18+
Previously it caused too much padding for descriptions with too short space-only lines.
19+
1. Improved subcommand help usage block - when `--help` is called for a subcommand, all manual usage lines
20+
include the whole path (subcommand values) starting from the topmost config.
1821

1922
## v2.0.0
2023

docs/todo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ The list of plans and ideas for future development.
158158
Let's try making major releases less frequent by accumulating here all ideas with backward incompatibilities.
159159
When the time comes, the whole bunch of stuff mentioned here will be implemented in a single major version.
160160

161+
1. Remove now obsolete `HelpGenerator::getBaseScriptName()`.
161162
1. Move to PHP 8.4 as a minimal required version. This includes:
162163
1. Replace `*trim()` functions with `mb_*trim()` alternatives.
163164

src/Parametizer/Config/HelpGenerator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,14 @@ public function getUsagesBlock(): string {
166166
// Print general usage template:
167167
$output .= $this->getUsageTemplate($this->config);
168168

169-
$usageExamples = $this->config->getUsageExamples();
170-
$baseScriptName = static::getBaseScriptName($this->config);
169+
$branchConfig = $this->config;
170+
$scriptNameParts = [];
171+
do {
172+
$scriptNameParts[] = $branchConfig->getScriptName();
173+
} while ($branchConfig = $branchConfig->getParent());
174+
$baseScriptName = implode(' ', array_reverse($scriptNameParts));
175+
176+
$usageExamples = $this->config->getUsageExamples();
171177

172178
// Firstly print usage examples with no description:
173179
foreach ($usageExamples as $usageExample) {

tests/Tests/Parametizer/HelpGenerator/HelpGeneratorTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public function testSubcommandScriptHelpFirstLevel(): void {
105105
106106
deep-nesting.php <switchme>
107107
108+
Very deep call:
109+
deep-nesting.php test11 --name-l2=supername test23 --name-l3=nameLevelThree test32
110+
108111
OPTIONS
109112
110113
--help Show full help page.
@@ -130,7 +133,7 @@ public function testSubcommandScriptHelpFirstLevel(): void {
130133
* Help for a specific subcommand.
131134
*
132135
* @see HelpGenerator::getFullHelp()
133-
* @see HelpGenerator::getBaseScriptName()
136+
* @see HelpGenerator::getUsagesBlock()
134137
*/
135138
public function testSubcommandScriptHelpDeepInSubcommand(): void {
136139
assertSame(
@@ -139,10 +142,10 @@ public function testSubcommandScriptHelpDeepInSubcommand(): void {
139142
USAGE
140143
141144
deep-nesting.php test11 [--name-l2=…] test23 [--name-l3=…] <switchme-l3>
142-
deep-nesting.php test11 --name-l2=supername test23 test31
145+
deep-nesting.php test11 test23 test31
143146
144147
Very deep call:
145-
deep-nesting.php test11 --name-l2=supername test23 --name-l3=nameLevelThree test32
148+
deep-nesting.php test11 test23 --name-l3=nameLevelThree test32
146149
147150
OPTIONS
148151

tests/Tests/Parametizer/HelpGenerator/scripts/deep-nesting.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
require_once __DIR__ . '/../../../init-console.php';
88

99
Parametizer::newConfig(throwOnException: true)
10+
// When you observe the whole config tree looking from the perspective of the very parent,
11+
// you may manually specify a deep subcommand usage example...
12+
->usage(
13+
'test11 --name-l2=supername test23 --name-l3=nameLevelThree test32',
14+
'Very deep call',
15+
)
16+
1017
->newSubcommandSwitch('switchme')
1118
->newSubcommand(
1219
'test11',
@@ -17,9 +24,15 @@
1724
->newSubcommand(
1825
'test23',
1926
Parametizer::newConfig(throwOnException: true)
20-
->usage('test11 --name-l2=supername test23 test31')
27+
/*
28+
* ... However when looking from the perspective of a branch, you should not be aware of parent
29+
* configs - think of branches as configs included automatically during runtime.
30+
* The point is that the help generator should detect correct paths up to the very top
31+
* and reflect it in the subcommand help usage block.
32+
*/
33+
->usage('test31')
2134
->usage(
22-
'test11 --name-l2=supername test23 --name-l3=nameLevelThree test32',
35+
'--name-l3=nameLevelThree test32',
2336
'Very deep call',
2437
)
2538

0 commit comments

Comments
 (0)