You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Pierre-Luc Lacroix (JIRA)" <ji...@apache.org> on 2014/09/17 18:12:34 UTC

[jira] [Comment Edited] (CLI-246) GnuParser removes first character from longer argument containing single character option

    [ https://issues.apache.org/jira/browse/CLI-246?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14137460#comment-14137460 ] 

Pierre-Luc Lacroix edited comment on CLI-246 at 9/17/14 4:12 PM:
-----------------------------------------------------------------

Emmanuel, I tried it with the trunk and I would argue it's just as bad with the DefaultParser but in a different way.


options: []
[[ option: cc  :: cc description :: class java.lang.String ], [ option: c  :: c description :: class java.lang.String ], [ option: c  :: c description :: class java.lang.String ], [ option: c  :: c description :: class java.lang.String ], [ option: c  :: c description :: class java.lang.String ]]

{code}
	@Test
	public void test3() throws ParseException {
		
		String [] cli = {"-cc", "-ccc", "-c"};
		
		Options opts = new Options();
		opts.addOption(new Option("cc", "cc description"));
		opts.addOption(new Option("c", "c description"));
		
		DefaultParser parser = new DefaultParser();
		CommandLine line = parser.parse(opts, cli, true);
		
		if (line.getArgs().length > 0 ) {
			Assert.assertEquals("-ccc", line.getArgList().get(0));
		}
		Assert.assertEquals(1, line.getArgs().length);
		Assert.assertEquals(2, line.getOptions().length);
	}
{code}


was (Author: pll.lacroix):
Emmanuel, I tried it with the trunk and I would argue it's just as bad with the DefaultParser but in a different way.


options: []
line: [[ option: cc  :: null :: class java.lang.String ], [ option: c  :: null :: class java.lang.String ], [ option: c  :: null :: class java.lang.String ], [ option: c  :: null :: class java.lang.String ], [ option: c  :: null :: class java.lang.String ]]

{code}
	@Test
	public void test3() throws ParseException {
		
		String [] cli = {"-cc", "-ccc", "-c"};
		
		Options opts = new Options();
		opts.addOption(new Option("cc", "cc description"));
		opts.addOption(new Option("c", "c description"));
		
		DefaultParser parser = new DefaultParser();
		CommandLine line = parser.parse(opts, cli, true);
		
		if (line.getArgs().length > 0 ) {
			Assert.assertEquals("-ccc", line.getArgList().get(0));
		}
		Assert.assertEquals(1, line.getArgs().length);
		Assert.assertEquals(2, line.getOptions().length);
	}
{code}

> GnuParser removes first character from longer argument containing single character option
> -----------------------------------------------------------------------------------------
>
>                 Key: CLI-246
>                 URL: https://issues.apache.org/jira/browse/CLI-246
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x
>    Affects Versions: 1.2
>         Environment: Windows 64-bit
>            Reporter: Pierre-Luc Lacroix
>            Priority: Minor
>
> Title might be hard to understand, but basically:
> Arguments:
> String [] cli = {"-cc", "-ccc", "-c"};
> Options:
> Options opts = new Options();
> opts.addOption(OptionBuilder.create("cc"));
> opts.addOption(OptionBuilder.create("c"));
> As you can see, "-ccc" is an extra unrecognized argument.
> When you try to parse it:
> CommandLineParser parser = new GnuParser();
> CommandLine line = parser.parse(opts, cli, true);
> Content on line:
> args: [cc, -c]
> line: [[ option: cc  :: null ], [ option: c  :: null ]]
> I would expect:
> args: [-ccc]
> line: [[ option: cc  :: null ], [ option: c  :: null ]]
> Wrote a few unit tests (3rd one fails):
> {code:title=UnitTest.java|borderStyle=solid}
> import org.apache.commons.cli.CommandLine;
> import org.apache.commons.cli.CommandLineParser;
> import org.apache.commons.cli.GnuParser;
> import org.apache.commons.cli.OptionBuilder;
> import org.apache.commons.cli.Options;
> import org.apache.commons.cli.ParseException;
> import org.junit.Assert;
> import org.junit.Test;
> public class CommonsCli {
> 	
> 	@Test
> 	public void test1() throws ParseException {
> 		
> 		String [] cli = {"-cc"};
> 		
> 		Options opts = new Options();
> 		opts.addOption(OptionBuilder.create("cc"));
> 		
> 		
> 		CommandLineParser parser = new GnuParser();
> 		CommandLine line = parser.parse(opts, cli);
> 		
> 		if (line.getArgs().length > 0 ) {
> 			Assert.fail("Extra argument found: " + line.getArgList().get(0));
> 		}
> 	}
> 	
> 	@Test
> 	public void test2() throws ParseException {
> 		
> 		String [] cli = {"-cc", "-ccc"};
> 		
> 		Options opts = new Options();
> 		opts.addOption(OptionBuilder.create("cc"));
> 		
> 		CommandLineParser parser = new GnuParser();
> 		CommandLine line = parser.parse(opts, cli, true);
> 		
> 		if (line.getArgs().length > 0 ) {
> 			Assert.assertEquals("-ccc", line.getArgList().get(0));
> 		}
> 	}
> 	
> 	
> 	@Test
> 	public void test3() throws ParseException {
> 		
> 		String [] cli = {"-cc", "-ccc", "-c"};
> 		
> 		Options opts = new Options();
> 		opts.addOption(OptionBuilder.create("cc"));
> 		opts.addOption(OptionBuilder.create("c"));
> 		
> 		CommandLineParser parser = new GnuParser();
> 		CommandLine line = parser.parse(opts, cli, true);
> 		
> 		if (line.getArgs().length > 0 ) {
> 			Assert.assertEquals("-ccc", line.getArgList().get(0));
> 		}
> 	}
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)