Skip to content

Commit 2c9858c

Browse files
committed
Add support for custom help screens
1 parent d3e7e76 commit 2c9858c

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class InitCommand extends Ahc\Cli\Input\Command
175175
->argument('[arg2]', 'The Arg2')
176176
->option('-a --apple', 'The Apple')
177177
->option('-b --ball', 'The ball')
178+
->help('Custom help screen (if omitted one will be generated)')
178179
// Usage examples:
179180
->usage(
180181
// append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown
@@ -243,6 +244,9 @@ $app->add(new OtherCommand, 'o');
243244
// Set logo
244245
$app->logo('Ascii art logo of your app');
245246

247+
// Custom help screen
248+
$app->help('Custom help screen (if omitted one will be generated)');
249+
246250
$app->handle($_SERVER['argv']); // if argv[1] is `i` or `init` it executes InitCommand
247251
```
248252

src/Application.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class Application
5252
/** @var string Ascii art logo */
5353
protected string $logo = '';
5454

55+
/** @var string Custom help screen */
56+
protected string $_help = '';
57+
58+
/** @var string Name of default command */
5559
protected string $default = '__default__';
5660

5761
/** @var null|Interactor */
@@ -344,9 +348,41 @@ protected function aliasesFor(Command $command): array
344348
}
345349

346350
/**
347-
* Show help of all commands.
351+
* Sets or gets the custom help screen contents.
352+
*
353+
* @param string|null $help
354+
*
355+
* @return string|self
356+
*/
357+
public function help(?string $help = null): mixed
358+
{
359+
if (func_num_args() === 0) {
360+
return $this->_help;
361+
}
362+
363+
$this->_help = $help;
364+
365+
return $this;
366+
}
367+
368+
/**
369+
* Show custom help screen if one is set, otherwise shows the default one.
348370
*/
349371
public function showHelp(): mixed
372+
{
373+
if ($help = $this->help()) {
374+
$writer = $this->io()->writer();
375+
$writer->write($help, true);
376+
return ($this->onExit)();
377+
}
378+
379+
return $this->showDefaultHelp();
380+
}
381+
382+
/**
383+
* Shows command help then aborts.
384+
*/
385+
public function showDefaultHelp(): mixed
350386
{
351387
$writer = $this->io()->writer();
352388
$header = "{$this->name}, version {$this->version}";

src/Input/Command.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Command extends Parser implements Groupable
5252

5353
protected ?string $_alias = null;
5454

55+
protected string $_help = '';
56+
5557
private array $_events = [];
5658

5759
private bool $_argVariadic = false;
@@ -291,9 +293,41 @@ protected function handleUnknown(string $arg, ?string $value = null): mixed
291293
}
292294

293295
/**
294-
* Shows command help then aborts.
296+
* Sets or gets the custom help screen contents.
297+
*
298+
* @param string|null $help
299+
*
300+
* @return string|self
301+
*/
302+
public function help(?string $help = null): mixed
303+
{
304+
if (func_num_args() === 0) {
305+
return $this->_help;
306+
}
307+
308+
$this->_help = $help;
309+
310+
return $this;
311+
}
312+
313+
/**
314+
* Show custom help screen if one is set, otherwise shows the default one.
295315
*/
296316
public function showHelp(): mixed
317+
{
318+
if ($help = $this->help()) {
319+
$writer = $this->io()->writer();
320+
$writer->write($help, true);
321+
return $this->emit('_exit', 0);
322+
}
323+
324+
return $this->showDefaultHelp();
325+
}
326+
327+
/**
328+
* Shows command help then aborts.
329+
*/
330+
public function showDefaultHelp(): mixed
297331
{
298332
$io = $this->io();
299333
$helper = new OutputHelper($io->writer());

tests/ApplicationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ public function test_help()
147147
$this->assertStringContainsString('stage change', $out);
148148
}
149149

150+
public function testCustomHelp()
151+
{
152+
$this->newApp('git', '0.0.2')
153+
->help('This should be my custom help screen')
154+
->parse(['git', '--help']);
155+
156+
$out = file_get_contents(static::$ou);
157+
158+
$this->assertStringContainsString('This should be my custom help screen', $out);
159+
}
160+
150161
public function test_action()
151162
{
152163
($a = $this->newApp('git', '0.0.2'))

tests/Input/CommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ public function __construct($tester)
284284
}
285285
};
286286
}
287+
288+
public function test_custom_help()
289+
{
290+
$p = $this->newCommand();
291+
$p->help('This should be my custom help screen');
292+
$this->assertStringContainsString('This should be my custom help screen', $p->help());
293+
}
287294

288295
protected function newCommand(string $version = '0.0.1', string $desc = '', bool $allowUnknown = false, $app = null)
289296
{

0 commit comments

Comments
 (0)