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

Issue parsing numeric options following an option which accepts multiple args in DefaultParser

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.4
    • None
    • CLI-1.x
    • None

    Description

      commons-cli seems to be unable to detect numeric options in their short "opt" form following an option which takes multiple arguments. 

      Will consistently throw:

      Exception in thread "main" org.apache.commons.cli.MissingOptionException: Missing required option: 2
      

      How to reproduce:

      Option multipleOptional = Option.builder("1")
          .longOpt("one")
          .argName("value1,value2,...,valueN")
          .hasArgs()
          .valueSeparator(',')
          .build();
      Option singleMandatory = Option.builder("2")
          .argName("value")
          .longOpt("two")
          .hasArg()
          .required()
          .build();
      
      Options options = new Options();
      options.addOption(singleMandatory);
      options.addOption(multipleOptional);
      
      CommandLineParser parser = new DefaultParser();
      CommandLine line = parser.parse(options, args);
      
      for (Option o : line.getOptions()) {
        System.out.println(o.getOpt() + '\t'
            + Arrays.toString(o.getValues()));
      }
      

      now pass in:

      --one argNumOne,argNumTwo -2 argNumThree 
      

      Note that an error will not occur if "opt" is set to a char like "a/b/c" or if the previous option is set with hasArg() instead of hasArgs()

      Also error will not occur if the longOpt is used such as:

      --one argNumOne,argNumTwo --two argNumThree 
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            Todd Ye Todd Ye
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: