-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
need way to send errors back from subcommand #672
Comments
So Picocli seems to have some exit code system, but I'm not understanding exactly how it works: @Command class ExitCodeDemo implements Runnable {
public void run() { throw new ParameterException(new CommandLine(this), "exit code demo"); }
public static void main(String... args) {
CommandLine cmd = new CommandLine(new ExitCodeDemo());
cmd.parseWithHandlers(
new RunLast().andExit(123),
CommandLine.defaultExceptionHandler().andExit(456),
args);
}
}
|
Yes, I agree that exit code handling needs to be improved. Very timely: I actually was just about to start working on this. Some earlier thinking is spread out over some older tickets (#561, #424, #541), let me pull it all together here. Goals
Current implementationPicocli currently has The existing Ideas for SolutionBorrowing from Spring Boot's exit code handling: IExitCodeGeneratorLet picocli provide an interface
IExitCodeExceptionMapperLet picocli provide an interface TBD: If the exception implements the Auto-documenting?It would be nice if the usage help message could automatically include an "Exit Status" section that lists the exit codes and their description. (This ticket explores some possibilities.) I'm beginning to think this is unrealistic. An alternative is to add API to let application authors provide a Entry pointAdd a new public static void main(String[] args) {
CommandLine cmd = new CommandLine(new App());
// map exceptions to exit codes
cmd.setExitCodeExceptionMapper(new MyMapper());
// add usage help "Exit Status" section
cmd.setExitStatusHelp(createMap("0:All normal", "1:Invalid input", "2:Disk full", "3:No network"));
// parse, invoke the business logic, handle errors and requests for help/version
int exitCode = cmd.execute(args);
System.exit(exitCode);
} The Error HandlingSplit up the current Execution FlowThe
|
…te` and `tryExecute` methods: configurable convenience methods with improved exit code support. * The new `execute` and `tryExecute` methods are similar to the `run`, `call` and `invoke` methods, but are not static, so they allow parser configuration. * In addition, these methods, in combination with the new `IExitCodeGenerator` and `IExitCodeExceptionMapper` interfaces, offer clean exit code support. * Finally, the `tryExecute` method rethrows any exception thrown from the Runnable, Callable or Method, while `execute` is guaranteed to never throw an exception. * Many variants of the previous `run`, `call` and `invoke` convenience methods are now deprecated in favor of the new `execute` methods. * Many methods on `AbstractHandler` are now deprecated. Still TODO: tests and documentation.
@garretwilson picocli-4.0-alpha-3 has been released with improved exit code support and exception handling. This is the last alpha! Please take a look and provide feedback if you have a chance. |
A CLI is supposed to return some exit code using e.g.
System.exit(2)
if there is an error. But this should be called at the top level, and not several levels deep.My application is calling the following to run the
Runnable
application:My application has a method-based subcommand—let's call it
bar
. If there is an error (e.g. let's say that a command-line switch was invalid), how does thebar()
method return an error so that the main application can callSystem.exit()
appropriately?On first glance it might appear that I should switch from using
Runnable
toCallable
. But this irrelevant for method subcommands, which are processed differently. I can't throw an exception, because Picocli hijacks the handling of any errors insideexecute(CommandLine parsed, List<Object> executionResult)
.It appears that Picocli is collecting any return value from the subcommand methods into
executionResult
. But how do I get those values back?(Personally I almost wish I could just throw my own exception from the subcommands and somehow bypass Picocli and let my higher-level application handle them.)
The text was updated successfully, but these errors were encountered: