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

Missing required options not throwing MissingOptionException

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 1.1
    • 1.2
    • Parser
    • None

    Description

      When an Options object is used to parse a second set of command arguments it won't throw a MissingOptionException.

      import org.apache.commons.cli.CommandLine;
      import org.apache.commons.cli.GnuParser;
      import org.apache.commons.cli.OptionBuilder;
      import org.apache.commons.cli.Options;
      import org.apache.commons.cli.ParseException;
      
      public class Example
      {
      	public static void main(String[] args) throws ParseException
      	{
      		brokenExample();
      		workingExample();
      	}
      
      	// throws exception as expected
      	private static void workingExample() throws ParseException
      	{
      		String[] args = {};
      
      		Options opts = new Options();
      		opts.addOption(OptionBuilder.isRequired().create('v'));
      
      		GnuParser parser = new GnuParser();
      		CommandLine secondCL = parser.parse(opts, args);
      
      		System.out.println("Done workingExample");
      	}
      
      	// fails to throw exception on second invocation of parse
      	private static void brokenExample() throws ParseException
      	{
      		String[] firstArgs = { "-v" };
      		String[] secondArgs = {};
      
      		Options opts = new Options();
      		opts.addOption(OptionBuilder.isRequired().create('v'));
      
      		GnuParser parser = new GnuParser();
      		CommandLine firstCL = parser.parse(opts, firstArgs);
      		CommandLine secondCL = parser.parse(opts, secondArgs);
      
      		System.out.println("Done brokenExample");
      	}
      }
      

      This is a result of the Options object returning the reference to its own list and the parsers modifying that list. The first call is removing the required options as they are found and subsequent calls get back an empty list.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            jwilde Jeremy Wilde
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Issue deployment