You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Andrew Ferguson <An...@arm.com> on 2004/07/15 19:39:50 UTC

commons-cli processing options incorrectly?

hi,

 this could be me misunderstanding the posix standard but this link

	
http://java.sun.com/docs/books/tutorial/essential/attributes/_posix.html

 says 

	"Options that do not require arguments can be grouped after a
hyphen, so, for example, -lst is equivalent to -t -l -s."


 but the following code:

	import org.apache.commons.cli.*;
	import java.util.*;
	
	public class CLITest {
		public static void main(String[]arg) throws Exception {
			CommandLineParser parser = new PosixParser();
	
			Options options = new Options();
			
			Option p = new Option("p", "pets", true, "takes
a list of up to 4 animals");
			p.setArgs(4);
			p.setRequired(true);
			
			Option v = new Option("v", "vet", true, "the
name of a vet");
			v.setArgs(100);
			v.setRequired(true);
			
			options.addOption(p).addOption(v);
	
			CommandLine line = parser.parse(options, arg);
			
			System.out.println("p ? "+line.hasOption("p")+"
"+Arrays.asList(line.getOptionValues("p")));
			System.out.println("v ? "+line.hasOption("v")+"
"+Arrays.asList(line.getOptionValues("v")));
		}
	}

 produces these results

	y:\java\misc>java CLITest -p 1 2 3 -v 4 5 6
	p ? true [1, 2, 3]
	v ? true [4, 5, 6]

	y:\java\misc>java CLITest -p 1 2 v 3 -v 4 5 6
	p ? true [1, 2]
	v ? true [4, 5, 6]
	
	y:\java\misc>java CLITest -p 1 v 2 3 -v 4 5 6
	p ? true [1]
	v ? true [4, 5, 6]

	y:\java\misc>java CLITest -p 1 V 2 3 -v 4 5 6
	p ? true [1, V, 2, 3]
	v ? true [4, 5, 6]

 when I was expecting: (* marks different from above)

	y:\java\misc>java CLITest -p 1 2 3 -v 4 5 6
	p ? true [1, 2, 3]
	v ? true [4, 5, 6]

	y:\java\misc>java CLITest -p 1 2 v 3 -v 4 5 6
*	p ? true [1, 2, v, 3] 
	v ? true [4, 5, 6]

	y:\java\misc>java CLITest -p 1 v 2 3 -v 4 5 6
*	p ? true [1, v, 2, 3]
	v ? true [4, 5, 6]

	y:\java\misc>java CLITest -p 1 V 2 3 -v 4 5 6
	p ? true [1, V, 2, 3]
	v ? true [4, 5, 6]

I'm constantly misunderstanding how command lines are meant to be
interpreted, but this seems wrong??

thanks,
Andrew

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org