shell.addSyntax("cmd", new OptionSetSyntax(new OptionSyntax("intArg", 'i'),
new OptionSyntax("fileArg", 'f'), new OptionSyntax("flagArg1", 'x'),
new OptionSyntax("flagArg2", 'y'), new OptionSyntax("flagArg3", 'z'),
new OptionSyntax("flagArg4", "boring")));
CommandLine cl;
CommandInfo cmdInfo;
Command cmd;
cl = new CommandLine(new Token("cmd"), new Token[] {}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
cl =
new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1")},
null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
.toString());
cl =
new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1"),
new Token("-x"), new Token("-yz")}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-yz")}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);
try {
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-xya")}, null);
cl.parseCommandLine(shell);
Assert.fail("no exception");
} catch (CommandSyntaxException ex) {
// expected
}
try {
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-")}, null);
cl.parseCommandLine(shell);
Assert.fail("no exception");
} catch (CommandSyntaxException ex) {
// expected
}
}