Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons CLI: how to prevent using short-name for options?

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.

like image 706
Francois Marot Avatar asked Jul 20 '16 10:07

Francois Marot


1 Answers

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);
like image 181
centic Avatar answered Sep 30 '22 18:09

centic