Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,15 @@ class InitCommand extends Ahc\Cli\Input\Command
{
parent::__construct('init', 'Init something');

$help = '<cyan>Custom help screen</end>';
$writer = new Ahc\Cli\Output\Writer();

$this
->argument('<arrg>', 'The Arrg')
->argument('[arg2]', 'The Arg2')
->option('-a --apple', 'The Apple')
->option('-b --ball', 'The ball')
->help($writer->colorizer()->colors($help))
// Usage examples:
->usage(
// append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown
Expand Down Expand Up @@ -243,6 +247,9 @@ $app->add(new OtherCommand, 'o');
// Set logo
$app->logo('Ascii art logo of your app');

// Custom help screen
$app->help('Custom help screen (if omitted one will be generated)');

$app->handle($_SERVER['argv']); // if argv[1] is `i` or `init` it executes InitCommand
```

Expand Down
39 changes: 38 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Application
/** @var string Ascii art logo */
protected string $logo = '';

/** @var string Custom help screen */
protected string $help = '';

/** @var string Name of default command */
protected string $default = '__default__';

/** @var null|Interactor */
Expand Down Expand Up @@ -344,9 +348,42 @@ protected function aliasesFor(Command $command): array
}

/**
* Show help of all commands.
* Sets or gets the custom help screen contents.
*
* @param string|null $help
*
* @return string|self
*/
public function help(?string $help = null): mixed
{
if (func_num_args() === 0) {
return $this->help;
}

$this->help = $help;

return $this;
}

/**
* Show custom help screen if one is set, otherwise shows the default one.
*/
public function showHelp(): mixed
{
if ($help = $this->help()) {
$writer = $this->io()->writer();
$writer->write($help, true);

return ($this->onExit)();
}

return $this->showDefaultHelp();
}

/**
* Shows command help then aborts.
*/
public function showDefaultHelp(): mixed
{
$writer = $this->io()->writer();
$header = "{$this->name}, version {$this->version}";
Expand Down
37 changes: 36 additions & 1 deletion src/Input/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Command extends Parser implements Groupable

protected ?string $_alias = null;

protected string $_help = '';

private array $_events = [];

private bool $_argVariadic = false;
Expand Down Expand Up @@ -291,9 +293,42 @@ protected function handleUnknown(string $arg, ?string $value = null): mixed
}

/**
* Shows command help then aborts.
* Sets or gets the custom help screen contents.
*
* @param string|null $help
*
* @return string|self
*/
public function help(?string $help = null): mixed
{
if (func_num_args() === 0) {
return $this->_help;
}

$this->_help = $help;

return $this;
}

/**
* Show custom help screen if one is set, otherwise shows the default one.
*/
public function showHelp(): mixed
{
if ($help = $this->help()) {
$writer = $this->io()->writer();
$writer->write($help, true);

return $this->emit('_exit', 0);
}

return $this->showDefaultHelp();
}

/**
* Shows command help then aborts.
*/
public function showDefaultHelp(): mixed
{
$io = $this->io();
$helper = new OutputHelper($io->writer());
Expand Down
11 changes: 11 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public function test_help()
$this->assertStringContainsString('stage change', $out);
}

public function testCustomHelp()
{
$this->newApp('git', '0.0.2')
->help('This should be my custom help screen')
->parse(['git', '--help']);

$out = file_get_contents(static::$ou);

$this->assertStringContainsString('This should be my custom help screen', $out);
}

public function test_action()
{
($a = $this->newApp('git', '0.0.2'))
Expand Down
7 changes: 7 additions & 0 deletions tests/Input/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ public function __construct($tester)
};
}

public function test_custom_help()
{
$p = $this->newCommand();
$p->help('This should be my custom help screen');
$this->assertStringContainsString('This should be my custom help screen', $p->help());
}

protected function newCommand(string $version = '0.0.1', string $desc = '', bool $allowUnknown = false, $app = null)
{
$p = new Command('cmd', $desc, $allowUnknown, $app);
Expand Down
Loading