Issue Details (XML | Word | Printable)

Key: CLI-156
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Trivial Trivial
Assignee: Unassigned
Reporter: Jeremy Wilde
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Commons CLI

Missing required options not throwing MissingOptionException

Created: 27/May/08 09:35 PM   Updated: 30/May/08 06:08 AM
Return to search
Component/s: Parser
Affects Version/s: 1.1
Fix Version/s: 1.2

Time Tracking:
Not Specified

Resolution Date: 29/May/08 01:39 PM


 Description  « Hide
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.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Henri Yandell made changes - 28/May/08 06:52 AM
Field Original Value New Value
Fix Version/s 1.2 [ 12312674 ]
Emmanuel Bourg made changes - 29/May/08 01:38 PM
Component/s Parser [ 12312374 ]
Component/s CLI-1.x [ 12311672 ]
Emmanuel Bourg made changes - 29/May/08 01:39 PM
Resolution Fixed [ 1 ]
Status Open [ 1 ] Resolved [ 5 ]
Henri Yandell made changes - 30/May/08 06:08 AM
Status Resolved [ 5 ] Closed [ 6 ]