Lanat Help

Sub-commands

Commands can contain sub-commands. This allows you to separate different logical parts of your program, which can have different arguments.

For example, a program like git has different sub-commands like git commit, git push, git pull, etc. These three are all different commands, but they do belong to the same parent command, git.

Let's take a look at how we can define sub-commands in Lanat.

Defining Sub-Commands

Simply add a new nested command template class to your command template class:

@Command.Define class MyCommand extends CommandTemplate { @Command.Define static class SubCommand extends CommandTemplate { } }

We are still missing something, as this code will make Lanat throw a CommandTemplateException. This is because at the time of gathering the parsed values, we have no way of actually accessing an instance of the sub-command class.

We need to add another property which will hold an instance of the sub-command class once it is parsed:

@Command.Define class MyCommand extends CommandTemplate { @Command.Define static class SubCommand extends CommandTemplate { @Argument.Define public int number; // we also added an argument to the sub-command } }

Defining Sub-Commands

You can add sub-commands to a command by using the Command#addCommand method:

var myCommand = new ArgumentParser("MyCommand") {{ addCommand(new Command("SubCommand") {{ // we also added an argument to the sub-command addArgument(Argument.create(new IntegerArgumentType(), "number")); }}); }};
Last modified: 21 April 2024