You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 66d7d2c
Browse filesBrowse the repository at this point in the historyBrowse files
UsageException is the error a handler throws and ParseException extends it; HelpRenderer::renderError() draws either with the help, and messages write code in backticks
An application catches one class and prints the message and the help of the
command in which the line went wrong by one call; two of the known consumers
printed no error of the command line at all, since the right way took five lines.
- UsageException carries the required $command; ParseException keeps reason,
parameter and token.
- renderError() writes "Error:" in the role `error`, the message with its code
spans, an empty line and the help of $e->command; the stream and the exit
code are the application's.
- formatText() draws the code spans of any text, the rest in a given color,
and without colors returns the text as it is; splitCodeSpans() is no longer
internal.
- The messages of the parser, of the enum check and of Normalizers write names,
commands and the values given as code spans by the internal formatCode(),
which escapes control characters and writes a value holding a backtick in a
longer run of them.
// --count 0 → throws "Option --count: expects at least 1, 0 given."
226
226
```
227
227
228
-
It runs only on values that really come from the command line: not on the default value, and not on the `true` of an option used without its optional value. A normalizer reports a bad value by throwing an exception. The parser turns it into a `ParseException` naming the parameter, so the user reads `Option --count: expects an integer, 'abc' given.` instead of a stack trace. An `\Error` is not caught, because a broken normalizer is a bug, not a bad value.
228
+
It runs only on values that really come from the command line: not on the default value, and not on the `true` of an option used without its optional value. A normalizer reports a bad value by throwing an exception. The parser turns it into a `ParseException` naming the parameter, so the user reads ``Option `--count`: expects an integer, `abc` given.`` instead of a stack trace. An `\Error` is not caught, because a broken normalizer is a bug, not a bad value.
229
229
230
230
`realPath()` resolves the value to an absolute path and refuses a path that does not exist, and an empty one:
231
231
@@ -473,37 +473,48 @@ When a standalone flag is used, the parser returns it as `true`, the other flags
473
473
Error Handling
474
474
==============
475
475
476
-
The parser throws `Nette\CommandLine\ParseException` for invalid user input. Its `command`property is the command in which the input went wrong, so you can show the right help:
476
+
The parser throws `Nette\CommandLine\ParseException` for invalid user input. It is a `UsageException`, which your handler throws for a mistake the definition cannot express, such as two options that cannot be combined. Its `command` is the command in which the input went wrong, and `renderError()` prints the message followed by the help of that command:
477
477
478
478
```php
479
-
use Nette\CommandLine\ParseException;
479
+
use Nette\CommandLine\UsageException;
480
480
481
481
try {
482
482
$args = (new Parser)->parse($cli);
483
-
} catch (ParseException $e) {
484
-
$stderr = new Console(STDERR);
485
-
$stderr->writeLine("Error: {$e->getMessage()}");
486
-
$stderr->writeLine();
487
-
(new HelpRenderer($stderr))->render($e->command);
483
+
exit($args->run());
484
+
} catch (UsageException $e) {
485
+
(new HelpRenderer(new Console(STDERR)))->renderError($e);
488
486
exit(2);
489
487
}
490
488
```
491
489
492
-
Its `reason` tells what was wrong, as a case of the `ParseFailure` enum, such as `ParseFailure::UnknownOption` or `ParseFailure::MissingArgument`, `parameter` is the parameter the error concerns, when there is one, and `token` the part of the command line it is about, as given: the name of an option as written, without its value, a command, the first unexpected argument or a bad value. An application can word the messages itself from these three; only a suggestion such as "Did you mean --verbose?" and the commands an option of another command belongs to are left to the message. The token is raw, so escape control characters before printing it.
490
+
A handler throws it with its command, which a parameter typed `Command` gets:
491
+
492
+
```php
493
+
function (Command $command, bool $stdin, array $paths): int {
494
+
if ($stdin && $paths) {
495
+
throw new UsageException('Paths cannot be combined with `--stdin`.', $command);
496
+
}
497
+
...
498
+
}
499
+
```
500
+
501
+
The message writes code in backticks, as a description does, and `renderError()` draws it in color without them. To print the message your own way, `formatText()` of the renderer does the same for any text.
502
+
503
+
The `reason` of a `ParseException` tells what was wrong, as a case of the `ParseFailure` enum, such as `ParseFailure::UnknownOption` or `ParseFailure::MissingArgument`, `parameter` is the parameter the error concerns, when there is one, and `token` the part of the command line it is about, as given: the name of an option as written, without its value, a command, the first unexpected argument or a bad value. An application can word the messages itself from these three; only a suggestion such as "Did you mean --verbose?" and the commands an option of another command belongs to are left to the message. The token is raw, so escape control characters before printing it.
493
504
494
505
Common error messages:
495
506
496
507
| Error | Cause |
497
508
|-------|-------|
498
-
|`Option --output requires a value.`| Option used without its required value |
499
-
|`Option --verbose does not accept a value.`| A value given to a flag |
500
-
|`Unknown option --verbos. Did you mean --verbose?`| Unrecognized option |
501
-
|`Missing required argument <file>.`| Required argument not provided |
502
-
|`Unexpected arguments 'b', 'c'.`| Extra positional arguments |
503
-
|`Option --format: expects json or xml, 'yaml' given.`| Value not in the enum |
504
-
|`Unknown command 'chekc'. Did you mean 'check'?`| Unrecognized command |
509
+
|``Option `--output` requires a value.``| Option used without its required value |
510
+
|``Option `--verbose` does not accept a value.``| A value given to a flag |
511
+
|``Unknown option `--verbos`. Did you mean `--verbose`?``| Unrecognized option |
512
+
|``Missing required argument `<file>`.``| Required argument not provided |
513
+
|``Unexpected arguments `b`, `c`.``| Extra positional arguments |
514
+
|``Option `--format`: expects `json` or `xml`, `yaml` given.``| Value not in the enum |
515
+
|``Unknown command `chekc`. Did you mean `check`?``| Unrecognized command |
505
516
|`Missing command.`| A command is required, but none was given |
506
-
|`Option --generate-baseline belongs to command 'check'.`| Option of another command |
517
+
|``Option `--generate-baseline` belongs to command `check`.``| Option of another command |
507
518
508
519
Errors in the definition itself, such as an option name without a dash, a name used twice or a required argument after an optional one, are programming errors and throw `\InvalidArgumentException`.
0 commit comments