airlift / airline

Java annotation-based framework for parsing Git like command line structures
Apache License 2.0
847 stars 138 forks source link

Missing required option doesn't throw #28

Closed jontodd closed 10 years ago

jontodd commented 10 years ago

This is a proposed fix for: https://github.com/airlift/airline/issues/22

This approach introduces a ParseResult object which allows for the collection of multiple parse errors from the user input and then provides this back to the caller to decide what to do. E.G. send the user the to the help dialog.

This keeps the existing API and fits with the desire to keep the parsing logic agnostic to the details of the HelpOption.

The existing pattern still fails fast with an exception:

MyCommand command = SingleCommand.singleCommand(MyCommand.class).parse(args);

This fix introduces the following pattern from the caller's perspective:

ParseResult parseResult = new ParseResult();
MyCommand command = SingleCommand.singleCommand(MyCommand.class).parse(args, parseResult);
command.helpOption.runOrShowHelp(parseResult, command);

The call to runOrShowHelp will: