Core\Command::help
Core\Command::help(?string $name): Core\Cli\TextCore\Command::help( ?string $name, ): Core\Cli\Text
Gives you the usage page of a command-line program, written from the commands the program declares.
You mark a method with #[Core\Command], and its parameters with #[Core\Option] where they are options. Novis reads those declarations while it compiles your program and builds a table from them. Core\Command::help turns that table into a page: the command, what it does, its arguments and its options, each with the type it was declared at. You never write usage text, so the page and the code cannot disagree.
Pass a command name for that one command's page. Pass null for the program's own page, which lists every command it has. A name the program declares no command for throws a LogicError.
Good to know: the result is a Core\Cli\Text, so you can print it with echo. The page already ends with a newline.
Parameters
$name?stringThe command to describe. Passnullto get the page that lists every command of the program.
Return value
Returns Core\Cli\Text. The page as a Core\Cli\Text, ending with a newline.
Errors
LogicErrorThe program has no#[Command]with the name$name. The list of commands is fixed when the program compiles, so this is a mistake in the program.
Examples
Every command your program has
<?nvs// Shows the page a program prints when somebody starts it with no command: the// list of commands it has.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy(string $target): uint { return 0; }
#[Core\Command(name: 'version', about: 'Print the version and stop')] public static function version(): uint { return 0; }}
// With `null`, `Core\Command::help` returns the program's own page. Both// commands above are on it, each with the text from its own `about:`.echo Core\Command::help(null);usage: <command> [arguments]
commands: deploy Push the current build version Print the version and stopOne commands arguments and options
<?nvs// Shows the page for one command: what the command takes, and what each part// of it is for.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy( // A parameter with no attribute is a positional argument. The page // writes it as `<target>`. string $target, // `#[Core\Option]` makes a parameter an option. The page writes every // spelling of it, the type, and the text from `about:`. #[Core\Option(short: 'n', about: 'Print what would happen')] bool $dryRun = false, #[Core\Option(about: 'How many times to try')] uint $retries = 3, ): uint { return 0; }}
// `Core\Command::help("deploy")` returns that one command's page. Every word// on it comes from the declaration above.echo Core\Command::help('deploy');usage: deploy <target> [--dryRun] [--retries]
Push the current build
arguments: <target> string
options: -n, --dryRun bool Print what would happen --retries uint = 3 How many times to tryYour own help command
<?nvs// Shows the everyday use: your program has its own `help` command, so somebody// can type `tool help deploy`. The name comes from the command line, so it can// be a word no command has. That throws, and the handler catches it.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy(string $target): uint { return 0; }
#[Core\Command(name: 'help', about: 'Show what a command does')] public static function help(string $command = ''): uint { try { // An empty name means somebody typed `tool help` and nothing else, // so print the program's own page. echo Core\Command::help($command == '' ? null : $command); } catch (LogicError $unknown) { echo 'there is no command called ', $command, "\n"; return 2; } return 0; }}
// This is what `tool help deploy` prints.Tool::help('deploy');
echo "\n";
// And this is what `tool help deply` prints. `deply` is not a command, so the// page throws and the handler prints one line instead.Tool::help('deply');usage: deploy <target>
Push the current build
arguments: <target> string
there is no command called deply