Uploaded image for project: 'Commons CLI'
  1. Commons CLI
  2. CLI-312

Inconsistent behaviour in key/value pairs (Java property style)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.5.0
    • 1.7.0
    • Parser
    • None

    Description

      In the Apache Maven project, we used to have an Option defined like this:

      Option.builder("D").longOpt("define").hasArg().build()
      

      This allowed our users to define properties using all of these:

      • -Dx=1
      • -Dx
      • -D x
      • --define x=1
      • --define x

      Splitting the key/value pairs was something that we did inside Maven (MavenCli.java #1733 and MavenCli.java #1762).

      In the process of upgrading to Commons CLI 1.5 and moving away from the deprecated GnuParser, we found out that Commons CLI can split argument values thanks to the valueSeparator. We've changed our option declaration accordingly:

      Option.builder("D").longOpt("define").numberOfArgs(2).valueSeparator('=').build() );
      

      Unfortunately, this breaks our accepted inputs: -Dv -Dw=1 -D x=2 -D y -D z=3 breaks the parsing in Commons CLI:

      org.apache.commons.cli.MissingArgumentException: Missing argument for option: D
      
      	at org.apache.commons.cli.DefaultParser.checkRequiredArgs(DefaultParser.java:231)
      	at org.apache.commons.cli.DefaultParser.handleOption(DefaultParser.java:394)
      	at org.apache.commons.cli.DefaultParser.handleShortAndLongOption(DefaultParser.java:467)
      	at org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:542)
      	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:714)
      	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:681)
      	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:662)
      

      From debugging, I've learned that it breaks at the fifth occurrence of -D (the one followed by z=3). It considers the fourth occurrence of -D incomplete (having only one value and not two). The strange thing is that the first occurrence of -D also has just one value (v) but parsing -Dw=1 does not break over that.

      I can submit a pull request for a bug that demonstrates this, but I have no clue how to address it. Also, I don't know if this is actually supposed to work. Looking forward to your reaction.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              mthmulders Maarten Mulders
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: