In the Apache Commons CLI lib, is it possible to bypass the usage of short-name, thus forcing the user to use the long name?
usually, options are defined like this:
new Option("u", "username", true, "automatic user name")
I would like to forbid the use of "u". But if I replace it with null
or empty string, there are Exceptions...
Why this requirement? I would like all my options to be only in the form --optionName=optionValue because some parts of my app are Spring Boot and Spring Boot recognizes by default options in this format.
Additionally, for consistency amongst devs and users and to simplify the documentation, I find it better if we have a unique way to use an option instead of 2.
Passing in null
as short name works for me when using the latest version of Apache Commons Cli 1.3.1 and the DefaultParser
(which is the only one not deprecated now):
Options options = new Options();
Option option = new Option(null, "help", true, "some desc");
options.addOption(option);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse( options, new String[] {"--help=foobar"});
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ant", options );
assertEquals(cmd.hasOption("help"), true);
String optionValue = cmd.getOptionValue("help");
assertEquals("foobar", optionValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With