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

MissingOptionException.getMissingOptions() returns OptionGroup, not just String

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2
    • Fix Version/s: 1.3
    • Component/s: CLI-1.x, Documentation
    • Labels:
      None

      Description

      The following code:

      Test.java
      import java.util.List;
      
      import org.apache.commons.cli.CommandLine;
      import org.apache.commons.cli.CommandLineParser;
      import org.apache.commons.cli.GnuParser;
      import org.apache.commons.cli.MissingOptionException;
      import org.apache.commons.cli.Option;
      import org.apache.commons.cli.OptionBuilder;
      import org.apache.commons.cli.OptionGroup;
      import org.apache.commons.cli.Options;
      
      public class Test {
      	public static void main(String[] argv)
      	{
      		Option opt_foo =
      			OptionBuilder.hasArg(false)
      				.isRequired(true)
      				.withDescription("option foo")
      		        .create("foo");
      
      		Option opt_bar =
      			OptionBuilder.hasArg(false)
      				.isRequired(false)
      				.withDescription("option bar")
      		        .create("bar");
      
      		Option opt_baz =
      			OptionBuilder.hasArg(false)
      				.isRequired(false)
      				.withDescription("option baz")
      		        .create("baz");
      
      		OptionGroup optgrp = new OptionGroup();
      		optgrp.setRequired(true);
      		optgrp.addOption(opt_bar)
      			.addOption(opt_baz);
      
      		Options optsdef = new Options();
      		optsdef.addOption(opt_foo)
      			.addOptionGroup(optgrp);
      
      		try {
      			CommandLineParser parser = new GnuParser();
      			CommandLine cmdline = parser.parse(optsdef, argv);
      		}
      		catch (MissingOptionException ex) {
      			List opts = ex.getMissingOptions();
      
      			for (Object option : opts) {
      				System.out.println("OPT: " + option.getClass().getName());
      			}
      		}
      		catch (Exception ex) {
      			ex.printStackTrace();
      			System.exit(1);
      		}
      	}
      }
      

      produces the following output:

      </tmp/MissingOptionException> $ javac -cp commons-cli-1.2.jar Test.java
      
      </tmp/MissingOptionException> $ java -cp commons-cli-1.2.jar:. Test
      OPT: java.lang.String
      OPT: org.apache.commons.cli.OptionGroup
      

      The JavaDoc for MissingOptionException.getMissingOptions() says:

      Return the list of options (as strings) missing in the command line parsed.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              joe.casadonte Joe Casadonte
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: