Core\Command::run
Core\Command::run(): uintRuns a command-line program: it reads the words the program was started with, calls the command they name, and gives you the status to end with.
You mark each command's method with #[Core\Command]. Novis builds a table of them while it compiles your program, and Core\Command::run matches the command line against that table. The first word chooses the command. The words after it fill that command's parameters, converted to the types the parameters were declared at.
The result is the number the handler returned, or 0 for a handler declared void. A command line this program cannot act on is a usage error: the usage page goes to standard error and the result is 2. Your program gives that number to exit.
Good to know: a command whose handler the program does not declare, or a parameter of a type no word can be converted into, throws a LogicError. Those are mistakes in the program, not in what the user typed.
Return value
Returns uint. The uint that the method returns, or 0 when the method returns void. A command line that matches no command is a usage error. Then the help page is printed to standard error, and the status is 2.
Errors
LogicErrorThe method of the command is not a method of this program. It is also thrown when a parameter has a type that a command-line argument cannot be converted to yet. Both are mistakes in the program.
Examples
One command and one call
<?nvs// Shows the smallest command-line program: one command, and one call that runs// it. Start it with `nvs run 01-one-command-and-one-call.nvs greet World` to// see the command run.
class Tool{ #[Core\Command(name: 'greet', about: 'Say hello to somebody')] public static function greet(string $name): uint { echo 'hello ', $name, "\n"; return 0; }}
// `Core\Command::run` reads the words the program was started with and calls// the command the first word names.uint $status = Core\Command::run();
// With no words, `run` writes the usage page to standard error and returns 2.// A real program gives this number to `exit`.echo 'status ', $status, "\n";status 2What the words become
<?nvs// Shows what the words on the command line become inside a command. Start it// with `nvs run 02-what-the-words-become.nvs deploy prod -n --retries 5` to see// all three values arrive.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy( // A parameter with no attribute takes the next word after the command. string $target, // A flag is `true` when it is written and `false` when it is not. #[Core\Option(short: 'n', about: 'Print what would happen')] bool $dryRun = false, // `5` arrives here as the number 5, because this parameter says `uint`. // A word that is not a number is a usage error. #[Core\Option(about: 'How many times to try')] uint $retries = 3, ): uint { echo 'target ', $target, "\n"; echo 'dry run ', $dryRun ? 'yes' : 'no', "\n"; echo 'retries ', $retries, "\n"; return 0; }}
// Started with no words, this writes the usage page to standard error and// returns 2, so nothing above runs.uint $status = Core\Command::run();echo 'status ', $status, "\n";status 2A tool with several commands
<?nvs// Shows the everyday use: a tool with several commands, where the status says// what happened. Start it with `nvs run 03-a-tool-with-several-commands.nvs// rollback` to see a command that ends with 3.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy(string $target): uint { echo 'deployed to ', $target, "\n"; return 0; }
#[Core\Command(name: 'rollback', about: 'Undo the last deploy')] public static function rollback(): uint { echo "there is nothing to undo\n"; // Your own number. `Core\Command::run` returns it unchanged, so a // script that started this program can test it. return 3; }
// A handler declared `void` always gives the status 0. #[Core\Command(name: 'version', about: 'Print the version and stop')] public static function version(): void { echo "1.4.0\n"; }}
// The status of a command line naming no command is 2, and the page listing// all three commands goes to standard error.uint $status = Core\Command::run();echo 'status ', $status, "\n";status 2