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

Unable to properly require options

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.2
    • 1.3
    • CLI-1.x, Options definition
    • None
    • windows jdk1.6.0_21

    Description

      Having some problems setting options as required. Have I misunderstood the documentation?

      • In test1() the option is required but also supplied, I expect to get no exceptions.
      • In test2() the option is required but not supplied, I expect to get an exception.
      public class Testing {
      
          public static void main(String[] args) {
              System.out.println("Begin test 1");
              test1();
              System.out.println("End test 1");
              System.out.println("Begin test 2");
              test2();
              System.out.println("End test 2");
          }
      
          private static void test1() {
      
              String[] args = new String[] { "--foo" };
      
              Options options = new Options();
              options.addOption("f", "foo", false, "");
              options.getRequiredOptions().add(options.getOption("foo"));
      
              GnuParser parser = new GnuParser();
              try {
                  parser.parse(options, args);
              } catch (ParseException e) {
                  // didn't expect to get MissingOptionException here
                  System.out.println("ERROR: " + e.getMessage());
              }
          }
      
          private static void test2() {
      
              String[] args = new String[] { };
      
              Options options = new Options();
              options.addOption("f", "foo", false, "");
              options.getOption("f").setRequired(true);
      
              GnuParser parser = new GnuParser();
              try {
                  parser.parse(options, args);
              } catch (ParseException e) {
                  // expected to get MissingOptionException here
                  System.out.println("ERROR: " + e.getMessage());
              }
          }
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            mizipzor Alexander Fast
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: