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

DefaultParser fails to parse token "-S=V" if short option is longer than 1 char

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.4
    • None
    • Parser
    • None
    • Windows 10, Java 11

    Description

      Assuming there is a short option with foo with one argument. Trying to parse a token like -foo=bar results in an UnrecognizedOptionException.

      The same scenario works fine if the short option is only one char long.

      The reason is an if-statement in DefaultParser.handleShortAndLongOption() (line 543 in v1.4), which checks opt.length() == 1 before checking if the option exists. IMO, this check should instead be isShortOption(opt).

      The below code demonstrates the issue.

      Options options = new Options();
      Option option = Option.builder("foo").hasArg().build();
      options.addOption(option);

      String[] args = new String[] {"-foo=bar"};

      DefaultParser parser = new DefaultParser();
      CommandLine cmdLine = parser.parse(options, args);

      assertEquals("bar", cmdLine.getOptionValue("foo"));

      Attachments

        Activity

          People

            Unassigned Unassigned
            BalzGuenat Balz Guenat
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: