diff options
author | Cedric Beust <cedric@beust.com> | 2012-08-16 10:33:26 -0700 |
---|---|---|
committer | Cedric Beust <cedric@beust.com> | 2012-08-16 10:33:26 -0700 |
commit | 8a4c3d11d8d74628fbe55dce61771810a841723e (patch) | |
tree | 2982e4ca111f5b9a547a14766f0cff50e9c1ea77 /src | |
parent | 80b44a9210ee3fe50e64eb4bdcaad03288ea9f08 (diff) | |
download | platform_external_jcommander-8a4c3d11d8d74628fbe55dce61771810a841723e.tar.gz platform_external_jcommander-8a4c3d11d8d74628fbe55dce61771810a841723e.tar.bz2 platform_external_jcommander-8a4c3d11d8d74628fbe55dce61771810a841723e.zip |
Don't throw an exception if running in "no validation" mode.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/beust/jcommander/JCommander.java | 25 | ||||
-rw-r--r-- | src/test/java/com/beust/jcommander/CmdTest.java | 86 | ||||
-rw-r--r-- | src/test/resources/testng-single.xml | 5 | ||||
-rw-r--r-- | src/test/resources/testng.xml | 1 |
4 files changed, 102 insertions, 15 deletions
diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index 30b9b4a..8965f3b 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -273,7 +273,7 @@ public class JCommander { if (m_descriptions == null) createDescriptions(); initializeDefaultValues(); - parseValues(expandArgs(args)); + parseValues(expandArgs(args), validate); if (validate) validateOptions(); } @@ -666,7 +666,7 @@ public class JCommander { /** * Main method that parses the values and initializes the fields accordingly. */ - private void parseValues(String[] args) { + private void parseValues(String[] args, boolean validate) { // This boolean becomes true if we encounter a command, which indicates we need // to stop parsing (the parsing of the command will be done in a sub JCommander // object) @@ -756,15 +756,18 @@ public class JCommander { // // Command parsing // - if (jc == null) throw new MissingCommandException("Expected a command, got " + arg); - m_parsedCommand = jc.m_programName.m_name; - m_parsedAlias = arg; //preserve the original form - - // Found a valid command, ask it to parse the remainder of the arguments. - // Setting the boolean commandParsed to true will force the current - // loop to end. - jc.parse(subArray(args, i + 1)); - commandParsed = true; + if (jc == null && validate) { + throw new MissingCommandException("Expected a command, got " + arg); + } else if (jc != null){ + m_parsedCommand = jc.m_programName.m_name; + m_parsedAlias = arg; //preserve the original form + + // Found a valid command, ask it to parse the remainder of the arguments. + // Setting the boolean commandParsed to true will force the current + // loop to end. + jc.parse(subArray(args, i + 1)); + commandParsed = true; + } } } } diff --git a/src/test/java/com/beust/jcommander/CmdTest.java b/src/test/java/com/beust/jcommander/CmdTest.java new file mode 100644 index 0000000..6601193 --- /dev/null +++ b/src/test/java/com/beust/jcommander/CmdTest.java @@ -0,0 +1,86 @@ +package com.beust.jcommander; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class CmdTest { + + @Parameters(commandNames = "--cmd-one") + public static class CmdOne { + } + + @Parameters(commandNames = "--cmd-two") + class CmdTwo { + @Parameter + List<String> params = new java.util.LinkedList<String>(); + } + + public String parseArgs(boolean withDefault, String[] args) { + JCommander jc = new JCommander(); + jc.addCommand(new CmdOne()); + jc.addCommand(new CmdTwo()); + + if (withDefault) { + // First check if a command was given, when not prepend default + // command (--cmd-two") + // In version up to 1.23 JCommander throws an Exception in this + // line, + // which might be incorrect, at least its not reasonable if the + // method + // is named "WithoutValidation". + jc.parseWithoutValidation(args); + if (jc.getParsedCommand() == null) { + LinkedList<String> newArgs = new LinkedList<String>(); + newArgs.add("--cmd-two"); + newArgs.addAll(Arrays.asList(args)); + jc.parse(newArgs.toArray(new String[0])); + } + } else { + jc.parse(args); + } + return jc.getParsedCommand(); + } + + @DataProvider + public Object[][] testData() { + return new Object[][] { + new Object[] { "--cmd-one", false, new String[] { "--cmd-one" } }, + new Object[] { "--cmd-two", false, new String[] { "--cmd-two" } }, + new Object[] { "--cmd-two", false, + new String[] { "--cmd-two", "param1", "param2" } }, + // This is the relevant test case to test default commands + new Object[] { "--cmd-two", true, + new String[] { "param1", "param2" } } }; + } + + @Test(dataProvider = "testData") + public void testArgsWithoutDefaultCmd(String expected, + boolean requireDefault, String[] args) { + if (!requireDefault) { + Assert.assertEquals(parseArgs(false, args), expected); + } + } + + @Test(dataProvider = "testData", expectedExceptions = MissingCommandException.class) + public void testArgsWithoutDefaultCmdFail(String expected, + boolean requireDefault, String[] args) { + if (requireDefault) { + parseArgs(false, args); + } else { + throw new MissingCommandException("irrelevant test case"); + } + } + + // We do not expect a MissingCommandException! + @Test(dataProvider = "testData") + public void testArgsWithDefaultCmd(String expected, boolean requireDefault, + String[] args) { + Assert.assertEquals(parseArgs(true, args), expected); + } + +}
\ No newline at end of file diff --git a/src/test/resources/testng-single.xml b/src/test/resources/testng-single.xml index d981d70..db8497b 100644 --- a/src/test/resources/testng-single.xml +++ b/src/test/resources/testng-single.xml @@ -7,10 +7,7 @@ <!-- <class name="com.beust.jcommander.FinderTest" /> --> - <class name="com.beust.jcommander.command.CommandAliasTest"> - <methods> - <include name="clashingAliasesAreNotAllowed" /> - </methods> + <class name="com.beust.jcommander.CmdTest"> </class> </classes> </test> diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index c3592e0..f804418 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -16,6 +16,7 @@ <class name="com.beust.jcommander.MethodSetterTest" /> <class name="com.beust.jcommander.PositiveIntegerTest" /> <class name="com.beust.jcommander.FinderTest" /> + <class name="com.beust.jcommander.CmdTest" /> </classes> </test> |