Creating subcommands with commons-cli
I am trying to create a simple argument parser using commons-cli and I cannot figure out how to create the following parameters:
java ... com.my.path.to.MyClass producer
java ... com.my.path.to.MyClass consumer -j 8
The first argument to my program must be either producer
or consumer
, which specifies the mode in which my program will run. If it is in mode consumer
, I would like to have an argument -j
that specifies the number of threads to serve.
Here's what I have so far:
Options options = new Options();
options.addOption("mode", false, "Things.");
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("startup.sh", options);
When I print these parameters, the parameter is mode
displayed as -mode
.
In Python, argparse
I would simply do the following:
parser = argparse.ArgumentParser()
parser.add_argument('mode', choices=('producer', 'consumer'), required=True)
parser.print_help()
This is exactly what I am looking for. How can I do this in commons-cli?
source to share