Let's assume we are working on a sample application that will receive two number arguments, and an operator argument. The application will then perform the operation on the two numbers and print the result.
We will make the operation argument optional, and if it is not provided, the application will default to addition.
Examples
$ my-program 5 10.12
The application will print 15.12.
$ my-program 5.2 10 --op subtract
The application will print -4.8.
In this tutorial we will not dwell into the details of all the options used. You can learn more about them in the documentation.
Defining the operations
Let's create an enum that will make our job easier when defining the operations.
public enum Operation {
@EnumArgumentType.Default
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE;
public double execute(double a, double b) {
return switch (this) {
case ADD -> a + b;
case SUBTRACT -> a - b;
case MULTIPLY -> a * b;
case DIVIDE -> a / b;
};
}
}
Defining the Command template
Now let's create a Command template, which will define the structure of our command.
@Command.Define
public class MyProgram extends CommandTemplate {
@Argument.Define(required = true, positional = true)
public double a;
@Argument.Define(required = true, positional = true)
public double b;
@Argument.Define
public Operation op;
}
The main function
Lastly, we'll write the main function that will parse the arguments and execute the operation.
public static void main(String[] args) {
var result = ArgumentParser.parseFromInto(MyProgram.class, args);
System.out.println(result.op.execute(result.a, result.b));
}
Result
Running the program now will result in the following output:
Conclusion
That's it! You've just created your first argument parser with Lanat. We've just scratched the surface of what Lanat can do. You can learn more about Lanat by reading the rest of the documentation. Have fun!
Complete example source code
@Command.Define
public class MyProgram extends CommandTemplate {
@Argument.Define(required = true, positional = true)
public double a;
@Argument.Define(required = true, positional = true)
public double b;
@Argument.Define
public Operation op;
public enum Operation {
@EnumArgumentType.Default
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE;
public double execute(double a, double b) {
return switch (this) {
case ADD -> a + b;
case SUBTRACT -> a - b;
case MULTIPLY -> a * b;
case DIVIDE -> a / b;
};
}
}
// entry point here
public static void main(String[] args) {
var result = ArgumentParser.parseFromInto(MyProgram.class, args);
System.out.println(result.op.execute(result.a, result.b));
}
}