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

[cli] A weakness of parser

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.1
    • Component/s: CLI-1.x
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: All

    • Bugzilla Id:
      39140

      Description

      I found a weakness of Jakarta Commons CLI and want to explain it with a simple
      example:

      Our program provides 2 options:

      1. -a or --algo <name>: The -a option requires an argument.
      2. -k or --key <value>: The -k option requires an argument too.

      a)

      If you pass the following command line arguments everything will be ok:
      -a Caesar -k A

      After evaluation:

      • "Caesar" is the parameter of the -a option and
      • "A" is the parameter of the -k option.

      b)

      However an org.apache.commons.cli.MissingArgumentException: no argument for:k is
      thrown if you pass the following input:

      -a Caesar -k a

      The Parser assumes that the argument "a" after the -k option, is the -a option
      missing the hyphen. At the end of this description there is Java code for
      executing this problem.

      Information:

      The handling of this command line

      -a Caesar -k a

      works in Getopt without any problem:

      • "Caesar" is the parameter of the -a option and
      • "a" of the -k option.

      After parsing a valid option Getopt always takes the next (available) command
      line argument as the option's parameter if the option requires an argument -
      means if you pass to the command line

      -k -a Caesar

      After evaluation:

      • "a" is the parameter of the -k option
      • the "Caesar" argument is just ignored

      If the option's parameter (<value>) represents an optional argument the next
      argument is not required, if it represents a valid option - means if you pass to
      the command line

      -k -a Caesar

      After evaluation:

      • "Caesar" is the parameter of the -a option
      • k option is set without a parameter - in this case a default value makes sense.

      Last but not least here is the code snippet for the CLI Test:

      import org.apache.commons.cli.CommandLine;
      import org.apache.commons.cli.CommandLineParser;
      import org.apache.commons.cli.Option;
      import org.apache.commons.cli.Options;
      import org.apache.commons.cli.ParseException;
      import org.apache.commons.cli.PosixParser;

      public class TestCommonsCLI {

      /**

      • @param args
        */
        public static void main(String[] args) {

      Options options = new Options();

      Option algorithm = new Option("a" , "algo", true, "the algorithm which it to
      perform executing");
      algorithm.setArgName("algorithm name");
      options.addOption(algorithm);

      Option key = new Option("k" , "key", true, "the key the setted algorithm uses
      to process");
      algorithm.setArgName("value");
      options.addOption(key);

      CommandLineParser parser = new PosixParser();

      try {
      CommandLine line = parser.parse( options, args);

      if(line.hasOption('a'))

      { System.out.println("algo: "+ line.getOptionValue( "a" )); }

      if(line.hasOption('k'))

      { System.out.println("key: " + line.getOptionValue('k')); }

      } catch (ParseException e)

      { // TODO Auto-generated catch block e.printStackTrace(); }

      }

      }

        Attachments

        1. Cloneable.patch
          13 kB
          Brian Egge
        2. CLI-71-fix.patch
          3 kB
          Henri Yandell
        3. CLI-71-badfix.patch
          2 kB
          Henri Yandell
        4. CLI71_resetvalues.patch
          6 kB
          Brian Egge
        5. BugCLI71Test.java
          2 kB
          Henri Yandell
        6. BugCLI71Test.java
          3 kB
          Henri Yandell
        7. ASF.LICENSE.NOT.GRANTED--TestCommonsCLI.java
          1 kB
          Amro Al-Akkad

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                amro.alakkad Amro Al-Akkad
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: