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

ArgumentBuilder.withMaximum causes parse errors: Unexpeced <value> while processing options

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.0
    • CLI-2.x
    • None
    • Windows XP

    Description

      With the sample program below, running with the arguments

      -a 0 -b 1 2 3 4

      causes the error:

      Unexpected 3 while processing options
      Usage:
      [-a <a1> [<a2> ...] -b <b1> <b2> [<b3> [<b4>]]]
      options
      a (-a) a [a ...]
      b (-b) b b [b [b]]

      I specified .withMinimum(2).withMaximum(4)
      The help is correct, but the value '3' is not added as a value.

      Note also that if for bOption, if you add two more defaults (uncomment them in the source)

      .withDefault("10000")
      .withDefault("1000000")

      an error is raised (incorrectly) on the first b value (i.e. 1 instead of 3) :

      Unexpected 1 while processing options
      Usage:
      [-a <a1> [<a2> ...] -b <b1> <b2> [<b3> [<b4>]]]
      options
      a (-a) a [a ...]
      b (-b) b b [b [b]]

      Source:

      package org.apache.commons.cli2.issues;

      import java.util.List;
      import org.apache.commons.cli2.CommandLine;
      import org.apache.commons.cli2.Group;
      import org.apache.commons.cli2.builder.ArgumentBuilder;
      import org.apache.commons.cli2.builder.DefaultOptionBuilder;
      import org.apache.commons.cli2.builder.GroupBuilder;
      import org.apache.commons.cli2.commandline.Parser;
      import org.apache.commons.cli2.option.DefaultOption;

      public class WithMinimum
      {
      public static void main(String[] args)

      { final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); final ArgumentBuilder abuilder = new ArgumentBuilder(); final GroupBuilder gbuilder = new GroupBuilder(); DefaultOption aOption = obuilder// .withShortName("a") .withLongName("a") .withArgument(abuilder .withName("a") .withDefault("10") .create()) .create(); DefaultOption bOption = obuilder .withShortName("b") .withLongName("b") .withArgument(abuilder .withName("b") .withMinimum(2) .withMaximum(4) .withDefault("100") .withDefault("1000") // .withDefault("10000") // .withDefault("1000000") .create()) .create(); Group options = gbuilder .withName("options") .withOption(aOption) .withOption(bOption) .create(); Parser parser = new Parser(); parser.setHelpTrigger("--help"); parser.setGroup(options); CommandLine cl = parser.parseAndHelp(args); if (cl == null) System.exit(1); int a = Integer.parseInt(cl.getValue(aOption).toString()); List b = cl.getValues(bOption); System.out.printf("a=%d b=%s%n", a, b); }

      }

      Attachments

        1. CLI145.patch
          7 kB
          Brian Egge

        Activity

          People

            Unassigned Unassigned
            djb David Biesack
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: