12

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.

1 Answer 1

11

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);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer @centric but I may be dumb, I don't understand how to query the 'cmd' object in case of a null short-arg. It seems all the methods on the CommandLine class require a short-name to be passed as parameter... Can you help me on this ? Thanks !
My code uses cmd.hasOption("help") to check if --help was given, does that work for you as well?
yes your code works well and I thank you for that. In fact I want to get the value of an Option. For exemple let's imagine that help may receive a value if declare like this: new Option(null, "help", false, "some desc"); Then how would I access the value given on the command line to "help" ? I fell like all the API's method need to use the short-arg to query for the value...
Did you try to set the "hasArgs" parameter to "true" for Option and then query it via cmd.getOptionValue("help")? In general please read a bit through the JavaDoc before posting follow-up questions that should be easy to find out. And please upvote and accept the answer as soon as it answers your original question.
Thanks Centric you were right. I was misleaded by the documentation which calls sometimes 'longopt' or 'name' the same thing... Here's a unit test below (or maybe you can edit your answer with this unit test which fits even best my original question). Thanks for all !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.