Lanat Help

Receiving the values

Once you have defined a command, you can use it to parse input from the user.

To parse input, call the parse() method that is present on the ArgumentParser instance:

void main(String[] args) { // creation of the command instance var myCommand = ArgumentParser.from(MyCommand.class); // parsing the input var afterParseOptions = myCommand.parse(CLInput.from(args)); }
void main(String[] args) { // creation of the command instance var myCommand = new ArgumentParser("MyCommand") {{ addArgument(Argument.create(new StringArgumentType(), "userName")); addArgument(Argument.create(new IntegerArgumentType(), "age")); }} // parsing the input var afterParseOptions = myCommand.parse(CLInput.from(args)); }

This will return a AfterParseOptions object, which allows you to do some post-parsing operations that can be useful.

The AfterParseOptions class

The AfterParseOptions class provides utility methods and defines a set of actions to be performed right before the parsed values are given. These actions are only executed if a terminator method is called.

Actions

Use the withActions method to define the actions that will be executed. The actions are defined by a lambda that receives an AfterParseActions instance.

var afterParseOptions = myCommand.parse(CLInput.from(args)); // define the actions method chain afterParseOptions.withActions(a -> a.printErrors().exitIfErrors());

These methods will be executed in the order they are chained. Here's a list of the available actions:

printErrors()

Prints all errors that occurred during parsing (Returned by AfterParseOptions#getErrors()) to System.err.

printHelpIfNoInput()

Prints the help message to System.out if no arguments were passed to the program.

exitIfErrors()

Exits the program with the error code returned by AfterParseOptions#getErrorCode() if any errors occurred during parsing.

exitIfNoInput()

Exits the program with a code of 0 if no arguments were passed to the program.

Terminator methods

Terminator methods are methods that will execute the actions defined in the AfterParseOptions once called before returning the actual parsed values. The available terminator methods are:

getResult()

Returns a ParseResultRoot object that contains all the parsed arguments.

into(Class)

Instantiates the given Command Template class and sets all the fields annotated with @Argument.Define corresponding to their respective parsed arguments. This method will also instantiate all the sub-commands recursively if defined in the template class properly.

Last modified: 05 May 2024