Lanat Help

State

Each element in a command hierarchy, when parsing begins, has a state, which can be altered while the parsing process is ongoing.

Lanat allows you to parse a command hierarchy more than once. After the first parse, all states are reset to their initial values so that the next parse can be done without any interference from the previous one.

var myCommand = ArgumentParser.from(MyCmd.class); var parsed1 = myCommand.parse(CLInput.args(args)); // parse var parsed2 = myCommand.parse(CLInput.args(args)); // reset -> parse var parsed3 = myCommand.parse(CLInput.args(args)); // reset -> parse

Resetting states on custom elements

Even if parsing multiple times is not common, we should always ensure that states are properly reset.

Elements that have the ability to reset their states (such as argument types) implement the Resettable interface. This interface has a single method, resetState(), which resets the state of the element.

Example

  1. Let's take a look at a simple example of a custom argument type.

    class MyType implements ArgumentType<String> { private int times = 0; @Override public String parseValues(String[] args) { return args[0] + " " + ++times; } @Override public Range getUsageCountBounds() { return Range.AT_LEAST_ONE; } }
  2. An issue arises when we parse the command multiple times. The times field will not be reset to its initial value, which is zero. In order to fix this, we need to implement our own resetState():

    @Override public void resetState() { super.resetState(); this.times = 0; }
Last modified: 31 May 2024