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:
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:
Defining Sub-Commands
You can add sub-commands to a command by using the Command#addCommand
method: