Lanat Help

ParseResult

When calling the getResult terminator method, you will receive a ParseResult object. This object allows you to easily access the parsed values.

Accessing the parsed values

  1. Call get and specify the argument whose value you want to access:

    // retrieve the value of the argument "arg" var argValue = result.<String>get("arg")
  2. argValue is an Optional<String> object. You may have to check if the value is present or not by using Optional's methods.

    argValue.ifPresent( value -> System.out.printf("Value of arg: %s%n", value) );
    String thing = argValue.orElse("unknown");

    See the Java Optional documentation for more information.

Accessing sub-command values

  • To access the values of a sub-command, you can use the same get method. The parameter is a var-args array of strings that represent the complete path of commands to reach the argument.

    var subCmdValue = result.<String>get("main-cmd", "subcmd", "arg") // ^ cmd ^ cmd ^ arg

See if a command was used

  • You can, on any ParseResult in the hierarchy, check if the command it belongs to was used by calling the wasUsed method:

    if (result.getSubResult("subcmd").wasUsed()) { System.out.println("Sub-command was used"); }

ParseResultRoot

The ParseResultRoot object is the root of the ParseResult hierarchy. It inherits ParseResult, thus it has the methods mentioned above, while also provides the following:

getForwardValue()

Returns an Optional<String> object that contains the forward value (if it was provided by the user).

getUsedResults()

Returns a list of all the ParseResult objects that belong to a command that was used by the user. The list is ordered from the root command to the last used command.

Here's an example of something you can do with this method:

result.getUsedResults() .forEach(res -> { switch (res.getCommand().getName()) { case "main" -> { System.out.println("main arg value: " + res.get("arg")); } case "sub-cmd" -> { System.out.println("arg num1 value: " + res.get("num1")); System.out.println("arg num2 value: " + res.get("num2")); } default -> {} }});
Last modified: 30 April 2024