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

[cli] PatternOptionBuilder does not support required Options

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0
    • 2.0
    • CLI-2.x
    • None
    • Operating System: Windows XP
      Platform: PC

    • 27575

    Description

      It seems like no required Options can be created via the
      PatternOptionBuilder. That is for the following reason:

      Usually "required" is being specified by including an "!"
      into the pattern. E.g. like this "hc!<" which should make
      the option "c" mandatory. But you always get an
      IllegalArgumentException because the "!" is used to create
      an Option. Here is the main loop...

      if (!isValueCode(ch))
      {
      if (opt != ' ')

      { options.addOption( OptionBuilder.hasArg(type != null) .isRequired(required).withType(type) .create(opt)); required = false; type = null; opt = ' '; }

      opt = ch;
      }
      else if (ch == '!')

      { required = true; }

      else

      { type = getValueClass(ch); }

      If you look at the code you can see the that "!"
      case is only being reached in the else clause.
      Which in turn means if isValueCode('!') is true.
      If you look at "isValueCode"

      public static boolean isValueCode(char ch)
      {
      if ((ch != '@') && (ch != ':') && (ch != '%') && (ch != '+')
      && (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*')
      && (ch != '/'))

      { return false; }

      return true;
      }

      you can see that "isValueCode('!')" will always return
      false and the else clause can never be reached.

      Adding the "!" to the "isValueCode" method should
      do the trick.

      public static boolean isValueCode(char ch)
      {
      if ((ch != '@') && (ch != ':') && (ch != '%') && (ch != '+')
      && (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*')
      && (ch != '/') && (ch != '!'))
      { return false; }

      return true;
      }

      Could someone please fix this?
      Thanks, Torsten

      Attachments

        Activity

          People

            Unassigned Unassigned
            tcurdt Torsten Curdt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: