summaryrefslogtreecommitdiffstats
path: root/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/beust/jcommander/ArgMultiNameValidator.java19
-rw-r--r--src/test/java/com/beust/jcommander/CmdTest.java29
-rw-r--r--src/test/java/com/beust/jcommander/ConverterFactoryTest.java22
-rw-r--r--src/test/java/com/beust/jcommander/DefaultProviderTest.java20
-rw-r--r--src/test/java/com/beust/jcommander/DefaultValueTest.java8
-rw-r--r--src/test/java/com/beust/jcommander/FinderTest.java11
-rw-r--r--src/test/java/com/beust/jcommander/HiddenConverter.java29
-rw-r--r--src/test/java/com/beust/jcommander/HiddenParameterSplitter.java31
-rw-r--r--src/test/java/com/beust/jcommander/JCommanderTest.java2560
-rw-r--r--src/test/java/com/beust/jcommander/MethodSetterTest.java9
-rw-r--r--src/test/java/com/beust/jcommander/ParametersDelegateTest.java6
-rw-r--r--src/test/java/com/beust/jcommander/ParametersNotEmptyTest.java37
-rw-r--r--src/test/java/com/beust/jcommander/PasswordTest.java111
-rw-r--r--src/test/java/com/beust/jcommander/PositiveIntegerTest.java10
-rw-r--r--src/test/java/com/beust/jcommander/SetConverter.java2
-rw-r--r--src/test/java/com/beust/jcommander/SimpleExample.java50
-rw-r--r--src/test/java/com/beust/jcommander/TypeHierarchyTest.java111
-rw-r--r--src/test/java/com/beust/jcommander/ValidatePropertiesWhenParsingTest.java3
-rw-r--r--src/test/java/com/beust/jcommander/VariableArityTest.java11
-rw-r--r--src/test/java/com/beust/jcommander/args/ArgsEnum.java4
-rw-r--r--src/test/java/com/beust/jcommander/args/ArgsLongCommandDescription.java12
-rw-r--r--src/test/java/com/beust/jcommander/args/ArgsLongMainParameterDescription.java17
-rw-r--r--src/test/java/com/beust/jcommander/args/ArgsMultiValidate.java36
-rw-r--r--src/test/java/com/beust/jcommander/args/HiddenArgs.java36
-rw-r--r--src/test/java/com/beust/jcommander/args/SlashSeparator.java2
-rw-r--r--src/test/java/com/beust/jcommander/args/VariableArity.java10
-rw-r--r--src/test/java/com/beust/jcommander/command/CommandNoParametersAnnotation.java9
-rw-r--r--src/test/java/com/beust/jcommander/command/CommandTest.java57
-rw-r--r--src/test/java/com/beust/jcommander/converters/CharArrayConverterTest.java37
-rw-r--r--src/test/java/com/beust/jcommander/converters/InetAddressConverterTest.java62
-rw-r--r--src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java5
31 files changed, 2253 insertions, 1113 deletions
diff --git a/src/test/java/com/beust/jcommander/ArgMultiNameValidator.java b/src/test/java/com/beust/jcommander/ArgMultiNameValidator.java
new file mode 100644
index 0000000..92a79bc
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/ArgMultiNameValidator.java
@@ -0,0 +1,19 @@
+package com.beust.jcommander;
+
+/**
+ * Created by jeremysolarz on 12/15/16.
+ */
+public class ArgMultiNameValidator {
+
+ public static class MultiNameValidator implements IValueValidator<String> {
+
+ public static String parsedName;
+
+ public void validate(String name, String value) throws ParameterException {
+ parsedName = name;
+ }
+ }
+
+ @Parameter(names = { "-name1", "-name2" }, description = "Names of parameter", validateValueWith = MultiNameValidator.class, required = true)
+ private String parameter;
+}
diff --git a/src/test/java/com/beust/jcommander/CmdTest.java b/src/test/java/com/beust/jcommander/CmdTest.java
index 6601193..229517d 100644
--- a/src/test/java/com/beust/jcommander/CmdTest.java
+++ b/src/test/java/com/beust/jcommander/CmdTest.java
@@ -17,7 +17,7 @@ public class CmdTest {
@Parameters(commandNames = "--cmd-two")
class CmdTwo {
@Parameter
- List<String> params = new java.util.LinkedList<String>();
+ List<String> params = new java.util.LinkedList<>();
}
public String parseArgs(boolean withDefault, String[] args) {
@@ -35,7 +35,7 @@ public class CmdTest {
// is named "WithoutValidation".
jc.parseWithoutValidation(args);
if (jc.getParsedCommand() == null) {
- LinkedList<String> newArgs = new LinkedList<String>();
+ LinkedList<String> newArgs = new LinkedList<>();
newArgs.add("--cmd-two");
newArgs.addAll(Arrays.asList(args));
jc.parse(newArgs.toArray(new String[0]));
@@ -70,7 +70,12 @@ public class CmdTest {
public void testArgsWithoutDefaultCmdFail(String expected,
boolean requireDefault, String[] args) {
if (requireDefault) {
- parseArgs(false, args);
+ try {
+ parseArgs(false, args);
+ } catch (MissingCommandException e) {
+ Assert.assertEquals(e.getUnknownCommand(), args[0]);
+ throw e;
+ }
} else {
throw new MissingCommandException("irrelevant test case");
}
@@ -83,4 +88,20 @@ public class CmdTest {
Assert.assertEquals(parseArgs(true, args), expected);
}
-} \ No newline at end of file
+ @Test
+ public void testIssue244() throws Exception {
+ class P1 {}
+ class P2 {
+ @Parameter(names = "--hello")
+ private int test;
+ }
+ P1 p1 = new P1();
+ P2 p2 = new P2();
+ JCommander j = new JCommander(p1);
+ j.addCommand("wonderful", p2);
+ j.setAllowAbbreviatedOptions(true);
+ j.parse("wond", "--he", "47");
+ Assert.assertEquals("wonderful", j.getParsedCommand());
+ Assert.assertEquals(47, p2.test);
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/ConverterFactoryTest.java b/src/test/java/com/beust/jcommander/ConverterFactoryTest.java
index e02166e..4b8b923 100644
--- a/src/test/java/com/beust/jcommander/ConverterFactoryTest.java
+++ b/src/test/java/com/beust/jcommander/ConverterFactoryTest.java
@@ -62,9 +62,10 @@ public class ConverterFactoryTest {
* Test that main parameters can be used with string converters,
* either with a factory or from the annotation.
*/
- private void mainWithHostPortParameters(IStringConverterFactory f, IHostPorts a) {
+ private void mainWithHostPortParameters(IStringConverterFactory f, IStringConverterInstanceFactory f2, IHostPorts a) {
JCommander jc = new JCommander(a);
if (f != null) jc.addConverterFactory(f);
+ if (f2 != null) jc.addConverterInstanceFactory(f2);
jc.parse("a.com:10", "b.com:20");
Assert.assertEquals(a.getHostPorts().get(0).host, "a.com");
Assert.assertEquals(a.getHostPorts().get(0).port.intValue(), 10);
@@ -74,12 +75,27 @@ public class ConverterFactoryTest {
@Test
public void mainWithoutFactory() {
- mainWithHostPortParameters(null, new ArgsMainParameter1());
+ mainWithHostPortParameters(null, null, new ArgsMainParameter2());
+ }
+
+ @Test(expectedExceptions = RuntimeException.class)
+ public void mainWithoutConverterWithoutFactory() {
+ mainWithHostPortParameters(null, null, new ArgsMainParameter1());
}
@Test
public void mainWithFactory() {
- mainWithHostPortParameters(CONVERTER_FACTORY, new ArgsMainParameter2());
+ mainWithHostPortParameters(CONVERTER_FACTORY, null, new ArgsMainParameter1());
+ }
+
+ @Test
+ public void mainWithInstanceFactory() {
+ mainWithHostPortParameters(null, new IStringConverterInstanceFactory() {
+ @Override
+ public IStringConverter<?> getConverterInstance(Parameter parameter, Class<?> forType, String optionName) {
+ return HostPort.class.equals(forType) ? new HostPortConverter() : null;
+ }
+ }, new ArgsMainParameter1());
}
}
diff --git a/src/test/java/com/beust/jcommander/DefaultProviderTest.java b/src/test/java/com/beust/jcommander/DefaultProviderTest.java
index 45ab6b6..45fad38 100644
--- a/src/test/java/com/beust/jcommander/DefaultProviderTest.java
+++ b/src/test/java/com/beust/jcommander/DefaultProviderTest.java
@@ -117,4 +117,24 @@ public class DefaultProviderTest {
Assert.assertEquals(a.log.intValue(), 19);
}
+ @Test
+ public void missingRequiredParameterWithDefaultValueProviderShouldNotRaiseParameterException() {
+ class ArgsRequired {
+ @Parameter(names = "-log", description = "Level of verbosity", required = true)
+ public Integer log;
+ }
+
+ IDefaultProvider defaultProvider = new IDefaultProvider() {
+ public String getDefaultValueFor(String optionName) {
+ return "-log".equals(optionName) ? "1" : "";
+ }
+ };
+
+ ArgsRequired a = new ArgsRequired();
+ JCommander jc = new JCommander(a);
+ jc.setDefaultProvider(defaultProvider);
+ jc.parse();
+
+ Assert.assertEquals(a.log.intValue(), 1);
+ }
}
diff --git a/src/test/java/com/beust/jcommander/DefaultValueTest.java b/src/test/java/com/beust/jcommander/DefaultValueTest.java
index 3b1f29c..403ecfd 100644
--- a/src/test/java/com/beust/jcommander/DefaultValueTest.java
+++ b/src/test/java/com/beust/jcommander/DefaultValueTest.java
@@ -36,7 +36,7 @@ public class DefaultValueTest {
public void emptyDefaultValueForListParameterStaysEmptyIfNotAssignedOrIsSetOtherwise() {
MyOptsWithEmptyDefaults opts = new MyOptsWithEmptyDefaults();
JCommander cmd = new JCommander(opts);
- cmd.parse(new String[]{"-a", "anotherValue"});
+ cmd.parse("-a", "anotherValue");
Assert.assertEquals(opts.list.size(), 1);
Assert.assertEquals(opts.list.get(0), "anotherValue");
Assert.assertEquals(opts.set.size(), 0);
@@ -46,7 +46,7 @@ public class DefaultValueTest {
public void defaultValueForListParametersGetsOverwrittenWithSpecifiedValueOrStaysAsDefaultOtherwise() {
MyOptsWithDefaultValues opts = new MyOptsWithDefaultValues();
JCommander cmd = new JCommander(opts);
- cmd.parse(new String[]{"-a", "anotherValue"});
+ cmd.parse("-a", "anotherValue");
Assert.assertEquals(opts.list.size(), 1);
Assert.assertEquals(opts.list.get(0), "anotherValue");
Assert.assertEquals(opts.set.size(), 1);
@@ -67,8 +67,8 @@ public class DefaultValueTest {
private void testSettingMultipleValuesToListTypeParameters(MyOpts opts) {
JCommander cmd = new JCommander(opts);
- cmd.parse(new String[]{"-a", "anotherValue", "-a", "anotherValue2",
- "-b", "anotherValue3", "-b", "anotherValue4"});
+ cmd.parse("-a", "anotherValue", "-a", "anotherValue2",
+ "-b", "anotherValue3", "-b", "anotherValue4");
Assert.assertEquals(opts.list.size(), 2);
Assert.assertEquals(opts.list.get(0), "anotherValue");
Assert.assertEquals(opts.list.get(1), "anotherValue2");
diff --git a/src/test/java/com/beust/jcommander/FinderTest.java b/src/test/java/com/beust/jcommander/FinderTest.java
index 94bf812..d4adda9 100644
--- a/src/test/java/com/beust/jcommander/FinderTest.java
+++ b/src/test/java/com/beust/jcommander/FinderTest.java
@@ -17,7 +17,7 @@ public class FinderTest {
Arg a = new Arg();
JCommander jc = new JCommander(a);
jc.setCaseSensitiveOptions(false);
- jc.parse(new String[] { "--PARAM", "foo" });
+ jc.parse("--PARAM", "foo");
Assert.assertEquals(a.param, "foo");
}
@@ -27,7 +27,6 @@ public class FinderTest {
JCommander jc = new JCommander(a);
jc.addCommand(conf);
jc.setCaseSensitiveOptions(false);
-// jc.setCaseSensitiveCommands(false);
jc.parse("--CONFIGURE");
String command = jc.getParsedCommand();
Assert.assertEquals(command, "--configure");
@@ -41,7 +40,7 @@ public class FinderTest {
Arg a = new Arg();
JCommander jc = new JCommander(a);
jc.setAllowAbbreviatedOptions(true);
- jc.parse(new String[] { "--par", "foo" });
+ jc.parse("--par", "foo");
Assert.assertEquals(a.param, "foo");
}
@@ -54,7 +53,7 @@ public class FinderTest {
JCommander jc = new JCommander(a);
jc.setCaseSensitiveOptions(false);
jc.setAllowAbbreviatedOptions(true);
- jc.parse(new String[] { "--PAR", "foo" });
+ jc.parse("--PAR", "foo");
Assert.assertEquals(a.param, "foo");
}
@@ -69,7 +68,7 @@ public class FinderTest {
Arg a = new Arg();
JCommander jc = new JCommander(a);
jc.setAllowAbbreviatedOptions(true);
- jc.parse(new String[] { "--par", "foo" });
+ jc.parse("--par", "foo");
Assert.assertEquals(a.param, "foo");
}
@@ -85,7 +84,7 @@ public class FinderTest {
JCommander jc = new JCommander(a);
jc.setCaseSensitiveOptions(false);
jc.setAllowAbbreviatedOptions(true);
- jc.parse(new String[] { "--PAR", "foo" });
+ jc.parse("--PAR", "foo");
Assert.assertEquals(a.param, "foo");
}
diff --git a/src/test/java/com/beust/jcommander/HiddenConverter.java b/src/test/java/com/beust/jcommander/HiddenConverter.java
new file mode 100644
index 0000000..cd36b85
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/HiddenConverter.java
@@ -0,0 +1,29 @@
+/**
+ * Copyright (C) 2010 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.beust.jcommander;
+
+public class HiddenConverter implements IStringConverter<String> {
+ private HiddenConverter() {
+ }
+
+ @Override
+ public String convert(String value) {
+ return value;
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/HiddenParameterSplitter.java b/src/test/java/com/beust/jcommander/HiddenParameterSplitter.java
new file mode 100644
index 0000000..a40f36f
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/HiddenParameterSplitter.java
@@ -0,0 +1,31 @@
+/**
+ * Copyright (C) 2010 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.beust.jcommander;
+
+import java.util.Arrays;
+import java.util.List;
+
+import com.beust.jcommander.converters.IParameterSplitter;
+
+public class HiddenParameterSplitter implements IParameterSplitter {
+ @Override
+ public List<String> split(String value) {
+ return Arrays.asList(value.split(";"));
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/JCommanderTest.java b/src/test/java/com/beust/jcommander/JCommanderTest.java
index ad2c5e8..e967ef7 100644
--- a/src/test/java/com/beust/jcommander/JCommanderTest.java
+++ b/src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -2,13 +2,13 @@
* Copyright (C) 2010 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
- *
+ * <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,1064 +18,1516 @@
package com.beust.jcommander;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.math.BigDecimal;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.EnumSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
-import java.util.TreeSet;
-
-import org.testng.Assert;
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
-
-import com.beust.jcommander.args.AlternateNamesForListArgs;
-import com.beust.jcommander.args.Args1;
-import com.beust.jcommander.args.Args1Setter;
-import com.beust.jcommander.args.Args2;
-import com.beust.jcommander.args.ArgsArityString;
-import com.beust.jcommander.args.ArgsBooleanArity;
-import com.beust.jcommander.args.ArgsBooleanArity0;
-import com.beust.jcommander.args.ArgsConverter;
-import com.beust.jcommander.args.ArgsEnum;
+import com.beust.jcommander.args.*;
import com.beust.jcommander.args.ArgsEnum.ChoiceType;
-import com.beust.jcommander.args.ArgsEquals;
-import com.beust.jcommander.args.ArgsHelp;
-import com.beust.jcommander.args.ArgsI18N1;
-import com.beust.jcommander.args.ArgsI18N2;
-import com.beust.jcommander.args.ArgsI18N2New;
-import com.beust.jcommander.args.ArgsInherited;
-import com.beust.jcommander.args.ArgsList;
-import com.beust.jcommander.args.ArgsMainParameter1;
-import com.beust.jcommander.args.ArgsMaster;
-import com.beust.jcommander.args.ArgsMultipleUnparsed;
-import com.beust.jcommander.args.ArgsOutOfMemory;
-import com.beust.jcommander.args.ArgsPrivate;
-import com.beust.jcommander.args.ArgsRequired;
-import com.beust.jcommander.args.ArgsSlave;
-import com.beust.jcommander.args.ArgsSlaveBogus;
-import com.beust.jcommander.args.ArgsValidate1;
-import com.beust.jcommander.args.ArgsWithSet;
-import com.beust.jcommander.args.Arity1;
-import com.beust.jcommander.args.SeparatorColon;
-import com.beust.jcommander.args.SeparatorEqual;
-import com.beust.jcommander.args.SeparatorMixed;
-import com.beust.jcommander.args.SlashSeparator;
-import com.beust.jcommander.args.VariableArity;
import com.beust.jcommander.command.CommandAdd;
import com.beust.jcommander.command.CommandCommit;
import com.beust.jcommander.command.CommandMain;
+import com.beust.jcommander.converters.FileConverter;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import java.io.*;
+import java.math.BigDecimal;
+import java.nio.charset.Charset;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.ResourceBundle;
@Test
public class JCommanderTest {
- public void simpleArgs() throws ParseException {
- Args1 args = new Args1();
- String[] argv = { "-debug", "-log", "2", "-float", "1.2", "-double", "1.3", "-bigdecimal", "1.4",
- "-date", "2011-10-26", "-groups", "unit", "a", "b", "c" };
- new JCommander(args, argv);
-
- Assert.assertTrue(args.debug);
- Assert.assertEquals(args.verbose.intValue(), 2);
- Assert.assertEquals(args.groups, "unit");
- Assert.assertEquals(args.parameters, Arrays.asList("a", "b", "c"));
- Assert.assertEquals(args.floa, 1.2f, 0.1f);
- Assert.assertEquals(args.doub, 1.3f, 0.1f);
- Assert.assertEquals(args.bigd, new BigDecimal("1.4"));
- Assert.assertEquals(args.date, new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-26"));
- }
-
- @DataProvider
- public Object[][] alternateNamesListArgs() {
- return new Object[][] {
- new String[][] {new String[] {"--servers", "1", "-s", "2", "--servers", "3"}},
- new String[][] {new String[] {"-s", "1", "-s", "2", "--servers", "3"}},
- new String[][] {new String[] {"--servers", "1", "--servers", "2", "-s", "3"}},
- new String[][] {new String[] {"-s", "1", "--servers", "2", "-s", "3"}},
- new String[][] {new String[] {"-s", "1", "-s", "2", "--servers", "3"}},
- };
- }
-
- /**
- * Confirm that List<?> parameters with alternate names return the correct
- * List regardless of how the arguments are specified
- */
-
- @Test(dataProvider = "alternateNamesListArgs")
- public void testAlternateNamesForListArguments(String[] argv) {
- AlternateNamesForListArgs args = new AlternateNamesForListArgs();
-
- new JCommander(args, argv);
-
- Assert.assertEquals(args.serverNames.size(), 3);
- Assert.assertEquals(args.serverNames.get(0), argv[1]);
- Assert.assertEquals(args.serverNames.get(1), argv[3]);
- Assert.assertEquals(args.serverNames.get(2), argv[5]);
- }
-
-
- /**
- * Make sure that if there are args with multiple names (e.g. "-log" and "-verbose"),
- * the usage will only display it once.
- */
- public void repeatedArgs() {
- Args1 args = new Args1();
- String[] argv = { "-log", "2" };
- JCommander jc = new JCommander(args, argv);
- Assert.assertEquals(jc.getParameters().size(), 8);
- }
-
- /**
- * Not specifying a required option should throw an exception.
- */
- @Test(expectedExceptions = ParameterException.class)
- public void requiredFields1Fail() {
- Args1 args = new Args1();
- String[] argv = { "-debug" };
- new JCommander(args, argv);
- }
-
- /**
- * Getting the description of a nonexistent command should throw an exception.
- */
- @Test(expectedExceptions = ParameterException.class)
- public void nonexistentCommandShouldThrow() {
- String[] argv = { };
- JCommander jc = new JCommander(new Object(), argv);
- jc.getCommandDescription("foo");
- }
-
- /**
- * Required options with multiple names should work with all names.
- */
- private void multipleNames(String option) {
- Args1 args = new Args1();
- String[] argv = { option, "2" };
- new JCommander(args, argv);
- Assert.assertEquals(args.verbose.intValue(), 2);
- }
-
- public void multipleNames1() {
- multipleNames("-log");
- }
-
- public void multipleNames2() {
- multipleNames("-verbose");
- }
-
- private void i18n1(String bundleName, Locale locale, String expectedString) {
- ResourceBundle bundle = locale != null ? ResourceBundle.getBundle(bundleName, locale)
- : null;
-
- ArgsI18N1 i18n = new ArgsI18N1();
- String[] argv = { "-host", "localhost" };
- JCommander jc = new JCommander(i18n, bundle, argv);
-// jc.usage();
-
- ParameterDescription pd = jc.getParameters().get(0);
- Assert.assertEquals(pd.getDescription(), expectedString);
- }
-
- public void i18nNoLocale() {
- i18n1("MessageBundle", null, "Host");
- }
-
- public void i18nUsLocale() {
- i18n1("MessageBundle", new Locale("en", "US"), "Host");
- }
-
- public void i18nFrLocale() {
- i18n1("MessageBundle", new Locale("fr", "FR"), "Hôte");
- }
-
- private void i18n2(Object i18n) {
- String[] argv = { "-host", "localhost" };
- Locale.setDefault(new Locale("fr", "FR"));
- JCommander jc = new JCommander(i18n, argv);
- ParameterDescription pd = jc.getParameters().get(0);
- Assert.assertEquals(pd.getDescription(), "Hôte");
- }
-
- public void i18nWithResourceAnnotation() {
- i18n2(new ArgsI18N2());
- }
-
- public void i18nWithResourceAnnotationNew() {
- i18n2(new ArgsI18N2New());
- }
-
- public void noParseConstructor() {
- JCommander jCommander = new JCommander(new ArgsMainParameter1());
- jCommander.usage(new StringBuilder());
- // Before fix, this parse would throw an exception, because it calls createDescription, which
- // was already called by usage(), and can only be called once.
- jCommander.parse();
- }
-
- /**
- * Test a use case where there are required parameters, but you still want
- * to interrogate the options which are specified.
- */
- public void usageWithRequiredArgsAndResourceBundle() {
- ArgsHelp argsHelp = new ArgsHelp();
- JCommander jc = new JCommander(new Object[]{argsHelp, new ArgsRequired()},
- java.util.ResourceBundle.getBundle("MessageBundle"));
- // Should be able to display usage without triggering validation
- jc.usage(new StringBuilder());
- try {
- jc.parse("-h");
- Assert.fail("Should have thrown a required parameter exception");
- } catch (ParameterException e) {
- Assert.assertTrue(e.getMessage().contains("are required"));
- }
- Assert.assertTrue(argsHelp.help);
- }
-
- public void multiObjects() {
- ArgsMaster m = new ArgsMaster();
- ArgsSlave s = new ArgsSlave();
- String[] argv = { "-master", "master", "-slave", "slave" };
- new JCommander(new Object[] { m , s }, argv);
-
- Assert.assertEquals(m.master, "master");
- Assert.assertEquals(s.slave, "slave");
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void multiObjectsWithDuplicatesFail() {
- ArgsMaster m = new ArgsMaster();
- ArgsSlave s = new ArgsSlaveBogus();
- String[] argv = { "-master", "master", "-slave", "slave" };
- new JCommander(new Object[] { m , s }, argv);
- }
-
- public void arityString() {
- ArgsArityString args = new ArgsArityString();
- String[] argv = { "-pairs", "pair0", "pair1", "rest" };
- new JCommander(args, argv);
-
- Assert.assertEquals(args.pairs.size(), 2);
- Assert.assertEquals(args.pairs.get(0), "pair0");
- Assert.assertEquals(args.pairs.get(1), "pair1");
- Assert.assertEquals(args.rest.size(), 1);
- Assert.assertEquals(args.rest.get(0), "rest");
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void arity2Fail() {
- ArgsArityString args = new ArgsArityString();
- String[] argv = { "-pairs", "pair0" };
- new JCommander(args, argv);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void multipleUnparsedFail() {
- ArgsMultipleUnparsed args = new ArgsMultipleUnparsed();
- String[] argv = { };
- new JCommander(args, argv);
- }
-
- public void privateArgs() {
- ArgsPrivate args = new ArgsPrivate();
- new JCommander(args, "-verbose", "3");
- Assert.assertEquals(args.getVerbose().intValue(), 3);
- }
-
- public void converterArgs() {
- ArgsConverter args = new ArgsConverter();
- String fileName = "a";
- new JCommander(args, "-file", "/tmp/" + fileName,
- "-listStrings", "Tuesday,Thursday",
- "-listInts", "-1,8",
- "-listBigDecimals", "-11.52,100.12");
- Assert.assertEquals(args.file.getName(), fileName);
- Assert.assertEquals(args.listStrings.size(), 2);
- Assert.assertEquals(args.listStrings.get(0), "Tuesday");
- Assert.assertEquals(args.listStrings.get(1), "Thursday");
- Assert.assertEquals(args.listInts.size(), 2);
- Assert.assertEquals(args.listInts.get(0).intValue(), -1);
- Assert.assertEquals(args.listInts.get(1).intValue(), 8);
- Assert.assertEquals(args.listBigDecimals.size(), 2);
- Assert.assertEquals(args.listBigDecimals.get(0), new BigDecimal("-11.52"));
- Assert.assertEquals(args.listBigDecimals.get(1), new BigDecimal("100.12"));
- }
-
- private void argsBoolean1(String[] params, Boolean expected) {
- ArgsBooleanArity args = new ArgsBooleanArity();
- new JCommander(args, params);
- Assert.assertEquals(args.debug, expected);
- }
-
- private void argsBoolean0(String[] params, Boolean expected) {
- ArgsBooleanArity0 args = new ArgsBooleanArity0();
- new JCommander(args, params);
- Assert.assertEquals(args.debug, expected);
- }
-
- public void booleanArity1() {
- argsBoolean1(new String[] {}, Boolean.FALSE);
- argsBoolean1(new String[] { "-debug", "true" }, Boolean.TRUE);
- }
-
- public void booleanArity0() {
- argsBoolean0(new String[] {}, Boolean.FALSE);
- argsBoolean0(new String[] { "-debug"}, Boolean.TRUE);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void badParameterShouldThrowParameter1Exception() {
- Args1 args = new Args1();
- String[] argv = { "-log", "foo" };
- new JCommander(args, argv);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void badParameterShouldThrowParameter2Exception() {
- Args1 args = new Args1();
- String[] argv = { "-long", "foo" };
- new JCommander(args, argv);
- }
-
- public void listParameters() {
- Args2 a = new Args2();
- String[] argv = {"-log", "2", "-groups", "unit", "a", "b", "c", "-host", "host2"};
- new JCommander(a, argv);
- Assert.assertEquals(a.verbose.intValue(), 2);
- Assert.assertEquals(a.groups, "unit");
- Assert.assertEquals(a.hosts, Arrays.asList("host2"));
- Assert.assertEquals(a.parameters, Arrays.asList("a", "b", "c"));
- }
-
- public void separatorEqual() {
- SeparatorEqual s = new SeparatorEqual();
- String[] argv = { "-log=3", "--longoption=10" };
- new JCommander(s, argv);
- Assert.assertEquals(s.log.intValue(), 3);
- Assert.assertEquals(s.longOption.intValue(), 10);
- }
-
- public void separatorColon() {
- SeparatorColon s = new SeparatorColon();
- String[] argv = { "-verbose:true" };
- new JCommander(s, argv);
- Assert.assertTrue(s.verbose);
- }
-
- public void separatorBoth() {
- SeparatorColon s = new SeparatorColon();
- SeparatorEqual s2 = new SeparatorEqual();
- String[] argv = { "-verbose:true", "-log=3" };
- new JCommander(new Object[] { s, s2 }, argv);
- Assert.assertTrue(s.verbose);
- Assert.assertEquals(s2.log.intValue(), 3);
- }
-
- public void separatorMixed1() {
- SeparatorMixed s = new SeparatorMixed();
- String[] argv = { "-long:1", "-level=42" };
- new JCommander(s, argv);
- Assert.assertEquals(s.l.longValue(), 1l);
- Assert.assertEquals(s.level.intValue(), 42);
- }
-
- public void slashParameters() {
- SlashSeparator a = new SlashSeparator();
- String[] argv = { "/verbose", "/file", "/tmp/a" };
- new JCommander(a, argv);
- Assert.assertTrue(a.verbose);
- Assert.assertEquals(a.file, "/tmp/a");
- }
-
- public void inheritance() {
- ArgsInherited args = new ArgsInherited();
- String[] argv = { "-log", "3", "-child", "2" };
- new JCommander(args, argv);
- Assert.assertEquals(args.child.intValue(), 2);
- Assert.assertEquals(args.log.intValue(), 3);
- }
-
- public void negativeNumber() {
- Args1 a = new Args1();
- String[] argv = { "-verbose", "-3" };
- new JCommander(a, argv);
- Assert.assertEquals(a.verbose.intValue(), -3);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void requiredMainParameters() {
- ArgsRequired a = new ArgsRequired();
- String[] argv = {};
- new JCommander(a, argv);
- }
-
- public void usageShouldNotChange() {
- JCommander jc = new JCommander(new Args1(), new String[]{"-log", "1"});
- StringBuilder sb = new StringBuilder();
- jc.usage(sb);
- String expected = sb.toString();
- jc = new JCommander(new Args1(), new String[]{"-debug", "-log", "2", "-long", "5"});
- sb = new StringBuilder();
- jc.usage(sb);
- String actual = sb.toString();
- Assert.assertEquals(actual, expected);
- }
-
- private void verifyCommandOrdering(String[] commandNames, Object[] commands) {
- CommandMain cm = new CommandMain();
- JCommander jc = new JCommander(cm);
-
- for (int i = 0; i < commands.length; i++) {
- jc.addCommand(commandNames[i], commands[i]);
- }
-
- Map<String, JCommander> c = jc.getCommands();
- Assert.assertEquals(c.size(), commands.length);
-
- Iterator<String> it = c.keySet().iterator();
- for (int i = 0; i < commands.length; i++) {
- Assert.assertEquals(it.next(), commandNames[i]);
- }
- }
-
- public void commandsShouldBeShownInOrderOfInsertion() {
- verifyCommandOrdering(new String[] { "add", "commit" },
- new Object[] { new CommandAdd(), new CommandCommit() });
- verifyCommandOrdering(new String[] { "commit", "add" },
- new Object[] { new CommandCommit(), new CommandAdd() });
- }
-
- @DataProvider
- public static Object[][] f() {
- return new Integer[][] {
- new Integer[] { 3, 5, 1 },
- new Integer[] { 3, 8, 1 },
- new Integer[] { 3, 12, 2 },
- new Integer[] { 8, 12, 2 },
- new Integer[] { 9, 10, 1 },
- };
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void arity1Fail() {
- final Arity1 arguments = new Arity1();
- final JCommander jCommander = new JCommander(arguments);
- final String[] commands = {
- "-inspect"
- };
- jCommander.parse(commands);
- }
-
- public void arity1Success1() {
- final Arity1 arguments = new Arity1();
- final JCommander jCommander = new JCommander(arguments);
- final String[] commands = {
- "-inspect", "true"
- };
- jCommander.parse(commands);
- Assert.assertTrue(arguments.inspect);
- }
-
- public void arity1Success2() {
- final Arity1 arguments = new Arity1();
- final JCommander jCommander = new JCommander(arguments);
- final String[] commands = {
- "-inspect", "false"
- };
- jCommander.parse(commands);
- Assert.assertFalse(arguments.inspect);
- }
-
- @Parameters(commandDescription = "Help for the given commands.")
- public static class Help {
- public static final String NAME = "help";
-
- @Parameter(description = "List of commands.")
- public List<String> commands=new ArrayList<String>();
- }
-
- @Test(expectedExceptions = ParameterException.class,
- description = "Verify that the main parameter's type is checked to be a List")
- public void wrongMainTypeShouldThrow() {
- JCommander jc = new JCommander(new ArgsRequiredWrongMain());
- jc.parse(new String[] { "f1", "f2" });
- }
-
- @Test(description = "This used to run out of memory")
- public void oom() {
- JCommander jc = new JCommander(new ArgsOutOfMemory());
- jc.usage(new StringBuilder());
- }
-
- @Test
- public void getParametersShouldNotNpe() {
- JCommander jc = new JCommander(new Args1());
- List<ParameterDescription> parameters = jc.getParameters();
- }
-
- public void validationShouldWork1() {
- ArgsValidate1 a = new ArgsValidate1();
- JCommander jc = new JCommander(a);
- jc.parse(new String[] { "-age", "2 "});
- Assert.assertEquals(a.age, new Integer(2));
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void validationShouldWorkWithDefaultValues() {
- ArgsValidate2 a = new ArgsValidate2();
- new JCommander(a);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void validationShouldWork2() {
- ArgsValidate1 a = new ArgsValidate1();
- JCommander jc = new JCommander(a);
- jc.parse(new String[] { "-age", "-2 "});
- }
-
- public void atFileCanContainEmptyLines() throws IOException {
- File f = File.createTempFile("JCommander", null);
- f.deleteOnExit();
- FileWriter fw = new FileWriter(f);
- fw.write("-log\n");
- fw.write("\n");
- fw.write("2\n");
- fw.close();
- new JCommander(new Args1(), "@" + f.getAbsolutePath());
- }
-
- public void handleEqualSigns() {
- ArgsEquals a = new ArgsEquals();
- JCommander jc = new JCommander(a);
- jc.parse(new String[] { "-args=a=b,b=c" });
- Assert.assertEquals(a.args, "a=b,b=c");
- }
-
- @SuppressWarnings("serial")
- public void handleSets() {
- ArgsWithSet a = new ArgsWithSet();
- new JCommander(a, new String[] { "-s", "3,1,2" });
- Assert.assertEquals(a.set, new TreeSet<Integer>() {{ add(1); add(2); add(3); }});
- }
-
- private static final List<String> V = Arrays.asList("a", "b", "c", "d");
-
- @DataProvider
- public Object[][] variable() {
- return new Object[][] {
- new Object[] { 0, V.subList(0, 0), V },
- new Object[] { 1, V.subList(0, 1), V.subList(1, 4) },
- new Object[] { 2, V.subList(0, 2), V.subList(2, 4) },
- new Object[] { 3, V.subList(0, 3), V.subList(3, 4) },
- new Object[] { 4, V.subList(0, 4), V.subList(4, 4) },
- };
- }
-
- @Test(dataProvider = "variable")
- public void variableArity(int count, List<String> var, List<String> main) {
- VariableArity va = new VariableArity(count);
- new JCommander(va).parse("-variable", "a", "b", "c", "d");
- Assert.assertEquals(var, va.var);
- Assert.assertEquals(main, va.main);
- }
-
- public void enumArgs() {
- ArgsEnum args = new ArgsEnum();
- String[] argv = { "-choice", "ONE", "-choices", "ONE", "Two" };
- JCommander jc = new JCommander(args, argv);
-
- Assert.assertEquals(args.choice, ArgsEnum.ChoiceType.ONE);
-
- List<ChoiceType> expected = Arrays.asList(ChoiceType.ONE, ChoiceType.Two);
- Assert.assertEquals(expected, args.choices);
- Assert.assertEquals(jc.getParameters().get(0).getDescription(),
- "Options: " + EnumSet.allOf((Class<? extends Enum>) ArgsEnum.ChoiceType.class));
-
- }
-
- public void enumArgsCaseInsensitive() {
- ArgsEnum args = new ArgsEnum();
- String[] argv = { "-choice", "one"};
- JCommander jc = new JCommander(args, argv);
-
- Assert.assertEquals(args.choice, ArgsEnum.ChoiceType.ONE);
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void enumArgsFail() {
- ArgsEnum args = new ArgsEnum();
- String[] argv = { "-choice", "A" };
- new JCommander(args, argv);
- }
-
- public void testListAndSplitters() {
- ArgsList al = new ArgsList();
- JCommander j = new JCommander(al);
- j.parse("-groups", "a,b", "-ints", "41,42", "-hp", "localhost:1000;example.com:1001",
- "-hp2", "localhost:1000,example.com:1001", "-uppercase", "ab,cd");
- Assert.assertEquals(al.groups.get(0), "a");
- Assert.assertEquals(al.groups.get(1), "b");
- Assert.assertEquals(al.ints.get(0).intValue(), 41);
- Assert.assertEquals(al.ints.get(1).intValue(), 42);
- Assert.assertEquals(al.hostPorts.get(0).host, "localhost");
- Assert.assertEquals(al.hostPorts.get(0).port.intValue(), 1000);
- Assert.assertEquals(al.hostPorts.get(1).host, "example.com");
- Assert.assertEquals(al.hostPorts.get(1).port.intValue(), 1001);
- Assert.assertEquals(al.hp2.get(1).host, "example.com");
- Assert.assertEquals(al.hp2.get(1).port.intValue(), 1001);
- Assert.assertEquals(al.uppercase.get(0), "AB");
- Assert.assertEquals(al.uppercase.get(1), "CD");
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void shouldThrowIfUnknownOption() {
- class A {
- @Parameter(names = "-long")
- public long l;
- }
- A a = new A();
- new JCommander(a).parse("-lon", "32");
- }
-
- @Test(expectedExceptions = ParameterException.class)
- public void mainParameterShouldBeValidate() {
- class V implements IParameterValidator {
-
- @Override
- public void validate(String name, String value) throws ParameterException {
- Assert.assertEquals("a", value);
- }
- }
-
- class A {
- @Parameter(validateWith = V.class)
- public List<String> m;
- }
-
- A a = new A();
- new JCommander(a).parse("b");
- }
-
- @Parameters(commandNames = { "--configure" })
- public static class ConfigureArgs {
- }
-
- public static class BaseArgs {
- @Parameter(names = { "-h", "--help" }, description = "Show this help screen")
- private boolean help = false;
-
- @Parameter(names = { "--version", "-version" }, description = "Show the program version")
- private boolean version;
- }
-
- public void commandsWithSamePrefixAsOptionsShouldWork() {
- BaseArgs a = new BaseArgs();
- ConfigureArgs conf = new ConfigureArgs();
- JCommander jc = new JCommander(a);
- jc.addCommand(conf);
- jc.parse("--configure");
- }
-
- // Tests:
- // required unparsed parameter
- @Test(enabled = false,
- description = "For some reason, this test still asks the password on stdin")
- public void askedRequiredPassword() {
- class A {
- @Parameter(names = { "--password", "-p" }, description = "Private key password",
- password = true, required = true)
- public String password;
-
- @Parameter(names = { "--port", "-o" }, description = "Port to bind server to",
- required = true)
- public int port;
- }
- A a = new A();
- InputStream stdin = System.in;
- try {
- System.setIn(new ByteArrayInputStream("password".getBytes()));
- new JCommander(a,new String[]{"--port", "7","--password"});
- Assert.assertEquals(a.port, 7);
- Assert.assertEquals(a.password, "password");
- } finally {
- System.setIn(stdin);
- }
- }
-
- public void dynamicParameters() {
- class Command {
- @DynamicParameter(names = {"-P"}, description = "Additional command parameters")
- private Map<String, String> params = Maps.newHashMap();
- }
- JCommander commander = new JCommander();
- Command c = new Command();
- commander.addCommand("command", c);
- commander.parse(new String[] { "command", "-Pparam='name=value'" });
- Assert.assertEquals(c.params.get("param"), "'name=value'");
- }
-
- public void exeParser() {
- class Params {
- @Parameter( names= "-i")
- private String inputFile;
- }
-
- String args[] = { "-i", "" };
- Params p = new Params();
- new JCommander(p, args);
- }
-
- public void multiVariableArityList() {
- class Params {
- @Parameter(names = "-paramA", description = "ParamA", variableArity = true)
- private List<String> paramA = Lists.newArrayList();
-
- @Parameter(names = "-paramB", description = "ParamB", variableArity = true)
- private List<String> paramB = Lists.newArrayList();
- }
-
- {
- String args[] = { "-paramA", "a1", "a2", "-paramB", "b1", "b2", "b3" };
- Params p = new Params();
- new JCommander(p, args).parse();
- Assert.assertEquals(p.paramA, Arrays.asList(new String[] { "a1", "a2" }));
- Assert.assertEquals(p.paramB, Arrays.asList(new String[] { "b1", "b2", "b3" }));
- }
-
- {
- String args[] = { "-paramA", "a1", "a2", "-paramB", "b1", "-paramA", "a3" };
- Params p = new Params();
- new JCommander(p, args).parse();
- Assert.assertEquals(p.paramA, Arrays.asList(new String[] { "a1", "a2", "a3" }));
- Assert.assertEquals(p.paramB, Arrays.asList(new String[] { "b1" }));
- }
- }
-
- @Test(enabled = false,
- description = "Need to double check that the command description is i18n'ed in the usage")
- public void commandKey() {
- @Parameters(resourceBundle = "MessageBundle", commandDescriptionKey = "command")
- class Args {
- @Parameter(names="-myoption", descriptionKey="myoption")
- private boolean option;
- }
- JCommander j = new JCommander();
- Args a = new Args();
- j.addCommand("comm", a);
- j.usage();
- }
-
- public void tmp() {
- class A {
- @Parameter(names = "-b")
- public String b;
- }
- new JCommander(new A()).parse("");
- }
-
- public void unknownOptionWithDifferentPrefix() {
- @Parameters(optionPrefixes = "/")
- class SlashSeparator {
-
- @Parameter(names = "/verbose")
- public boolean verbose = false;
-
- @Parameter(names = "/file")
- public String file;
- }
- SlashSeparator ss = new SlashSeparator();
- try {
- new JCommander(ss).parse("/notAParam");
- } catch (ParameterException ex) {
- boolean result = ex.getMessage().contains("Unknown option");
- Assert.assertTrue(result);
- }
- }
-
- public void equalSeparator() {
- @Parameters(separators = "=", commandDescription = "My command")
- class MyClass {
-
- @Parameter(names = { "-p", "--param" }, required = true, description = "param desc...")
- private String param;
- }
- MyClass c = new MyClass();
- String expected = "\"hello\"world";
- new JCommander(c).parse("--param=" + expected);
- Assert.assertEquals(expected, c.param);
- }
-
- public void simpleArgsSetter() throws ParseException {
- Args1Setter args = new Args1Setter();
- String[] argv = { "-debug", "-log", "2", "-float", "1.2", "-double", "1.3", "-bigdecimal", "1.4",
- "-date", "2011-10-26", "-groups", "unit", "a", "b", "c" };
- new JCommander(args, argv);
-
- Assert.assertTrue(args.debug);
- Assert.assertEquals(args.verbose.intValue(), 2);
- Assert.assertEquals(args.groups, "unit");
- Assert.assertEquals(args.parameters, Arrays.asList("a", "b", "c"));
- Assert.assertEquals(args.floa, 1.2f, 0.1f);
- Assert.assertEquals(args.doub, 1.3f, 0.1f);
- Assert.assertEquals(args.bigd, new BigDecimal("1.4"));
- Assert.assertEquals(args.date, new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-26"));
- }
-
- public void verifyHelp() {
- class Arg {
- @Parameter(names = "--help", help = true)
- public boolean help = false;
-
- @Parameter(names = "file", required = true)
- public String file;
- }
- Arg arg = new Arg();
- String[] argv = { "--help" };
- new JCommander(arg, argv);
-
- Assert.assertTrue(arg.help);
- }
-
- public void helpTest() {
- class Arg {
- @Parameter(names = { "?", "-help", "--help" }, description = "Shows help", help = true)
- private boolean help = false;
- }
- Arg arg = new Arg();
- JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "-help" });
-// System.out.println("helpTest:" + arg.help);
- }
-
- @Test(enabled = false, description = "Should only be enable once multiple parameters are allowed")
- public void duplicateParameterNames() {
- class ArgBase {
- @Parameter(names = { "-host" })
- protected String host;
- }
-
- class Arg1 extends ArgBase {}
- Arg1 arg1 = new Arg1();
-
- class Arg2 extends ArgBase {}
- Arg2 arg2 = new Arg2();
-
- JCommander jc = new JCommander(new Object[] { arg1, arg2});
- jc.parse(new String[] { "-host", "foo" });
- Assert.assertEquals(arg1.host, "foo");
- Assert.assertEquals(arg2.host, "foo");
- }
-
- public void parameterWithOneDoubleQuote() {
- @Parameters(separators = "=")
- class Arg {
- @Parameter(names = { "-p", "--param" })
- private String param;
- }
- JCommander jc = new JCommander(new MyClass());
- jc.parse("-p=\"");
- }
-
- public void emptyStringAsDefault() {
- class Arg {
- @Parameter(names = "-x")
- String s = "";
- }
- Arg a = new Arg();
- StringBuilder sb = new StringBuilder();
- new JCommander(a).usage(sb);
- Assert.assertTrue(sb.toString().contains("Default: <empty string>"));
- }
-
- public void spaces() {
- class Arg {
- @Parameter(names = "-rule", description = "rule")
- private List<String> rules = new ArrayList<String>();
- }
- Arg a = new Arg();
- new JCommander(a, "-rule", "some test");
- Assert.assertEquals(a.rules, Arrays.asList("some test"));
- }
-
- static class V2 implements IParameterValidator2 {
- final static List<String> names = Lists.newArrayList();
- static boolean validateCalled = false;
-
- @Override
- public void validate(String name, String value) throws ParameterException {
- validateCalled = true;
- }
-
- @Override
- public void validate(String name, String value, ParameterDescription pd)
- throws ParameterException {
- names.addAll(Arrays.asList(pd.getParameter().names()));
- }
- }
-
- public void validator2() {
- class Arg {
- @Parameter(names = { "-h", "--host" }, validateWith = V2.class)
- String host;
- }
- Arg a = new Arg();
- V2.names.clear();
- V2.validateCalled = false;
- JCommander jc = new JCommander(a, "--host", "h");
- jc.setAcceptUnknownOptions(true);
- Assert.assertEquals(V2.names, Arrays.asList(new String[] { "-h", "--host" }));
- Assert.assertTrue(V2.validateCalled);
- }
-
- public void usageCommandsUnderUsage() {
- class Arg {
- }
- @Parameters(commandDescription = "command a")
- class ArgCommandA {
- @Parameter(description = "command a parameters")
- List<String> parameters;
- }
- @Parameters(commandDescription = "command b")
- class ArgCommandB {
- @Parameter(description = "command b parameters")
- List<String> parameters;
- }
-
- Arg a = new Arg();
-
- JCommander c = new JCommander(a);
- c.addCommand("a", new ArgCommandA());
- c.addCommand("b", new ArgCommandB());
-
- StringBuilder sb = new StringBuilder();
- c.usage(sb);
- Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
- }
-
- public void usageWithEmpytLine() {
- class Arg {
- }
- @Parameters(commandDescription = "command a")
- class ArgCommandA {
- @Parameter(description = "command a parameters")
- List<String> parameters;
- }
- @Parameters(commandDescription = "command b")
- class ArgCommandB {
- @Parameter(description = "command b parameters")
- List<String> parameters;
- }
-
- Arg a = new Arg();
-
- JCommander c = new JCommander(a);
- c.addCommand("a", new ArgCommandA());
- c.addCommand("b", new ArgCommandB());
-
- StringBuilder sb = new StringBuilder();
- c.usage(sb);
- Assert.assertTrue(sb.toString().contains("command a parameters\n\n b"));
- }
-
- public void partialValidation() {
- class Arg {
- @Parameter(names = { "-h", "--host" })
- String host;
- }
- Arg a = new Arg();
- JCommander jc = new JCommander();
- jc.setAcceptUnknownOptions(true);
- jc.addObject(a);
- jc.parse("-a", "foo", "-h", "host");
- Assert.assertEquals(a.host, "host");
- Assert.assertEquals(jc.getUnknownOptions(), Lists.newArrayList("-a", "foo"));
- }
-
- /**
- * GITHUB-137.
- */
- public void listArgShouldBeCleared() {
- class Args {
- @Parameter(description = "[endpoint]")
- public List<String> endpoint = Lists.newArrayList("prod");
- }
- Args a = new Args();
- new JCommander(a, new String[] { "dev" });
- Assert.assertEquals(a.endpoint, Lists.newArrayList("dev"));
- }
-
- public void dashDashParameter() {
- class Arguments {
- @Parameter(names = { "-name" })
- public String name;
- @Parameter
- public List<String> mainParameters;
- }
-
- Arguments a = new Arguments();
- new JCommander(a, new String[] {
- "-name", "theName", "--", "param1", "param2"}
- );
- Assert.assertEquals(a.name, "theName");
- Assert.assertEquals(a.mainParameters.size(), 2);
- Assert.assertEquals(a.mainParameters.get(0), "param1");
- Assert.assertEquals(a.mainParameters.get(1), "param2");
- }
-
- public void dashDashParameter2() {
- class Arguments {
- @Parameter(names = { "-name" })
- public String name;
+
+ @Test
+ public void testLongMainParameterDescription() {
+ //setup
+ JCommander jc = new JCommander(new ArgsLongMainParameterDescription());
+ StringBuilder sb = new StringBuilder();
+
+ //action
+ jc.usage(sb);
+
+ //verify
+ for (String line : sb.toString().split("\n")) {
+ Assert.assertTrue(line.length() <= jc.getColumnSize(), "line length < column size");
+ }
+ }
+
+ @Test
+ public void testLongCommandDescription() throws Exception {
+ //setup
+ JCommander jc = new JCommander();
+ jc.addCommand(new ArgsLongCommandDescription());
+ StringBuilder sb = new StringBuilder();
+
+ //action
+ jc.usage(sb);
+
+ //verify
+ for (String line : sb.toString().split("\n")) {
+ Assert.assertTrue(line.length() <= jc.getColumnSize(), "line length < column size");
+ }
+ }
+
+ @Test
+ public void testDescriptionWrappingLongWord() {
+ //setup
+ StringBuilder sb = new StringBuilder();
+ final JCommander jc = new JCommander(new ArgsLongDescription());
+
+ //action
+ jc.usage(sb);
+
+ //verify
+ for (String line : sb.toString().split("\n")) {
+ Assert.assertTrue(line.length() <= jc.getColumnSize(), "line length < column size");
+ }
+ }
+
+ public void simpleArgs() throws ParseException {
+ Args1 args = new Args1();
+ String[] argv = {"-debug", "-log", "2", "-float", "1.2", "-double", "1.3", "-bigdecimal", "1.4",
+ "-date", "2011-10-26", "-groups", "unit", "a", "b", "c"};
+ new JCommander(args, argv);
+
+ Assert.assertTrue(args.debug);
+ Assert.assertEquals(args.verbose.intValue(), 2);
+ Assert.assertEquals(args.groups, "unit");
+ Assert.assertEquals(args.parameters, Arrays.asList("a", "b", "c"));
+ Assert.assertEquals(args.floa, 1.2f, 0.1f);
+ Assert.assertEquals(args.doub, 1.3f, 0.1f);
+ Assert.assertEquals(args.bigd, new BigDecimal("1.4"));
+ Assert.assertEquals(args.date, new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-26"));
+ }
+
+ @DataProvider
+ public Object[][] alternateNamesListArgs() {
+ return new Object[][]{
+ new String[][]{new String[]{"--servers", "1", "-s", "2", "--servers", "3"}},
+ new String[][]{new String[]{"-s", "1", "-s", "2", "--servers", "3"}},
+ new String[][]{new String[]{"--servers", "1", "--servers", "2", "-s", "3"}},
+ new String[][]{new String[]{"-s", "1", "--servers", "2", "-s", "3"}},
+ new String[][]{new String[]{"-s", "1", "-s", "2", "--servers", "3"}},
+ };
+ }
+
+ /**
+ * Confirm that List<?> parameters with alternate names return the correct
+ * List regardless of how the arguments are specified
+ */
+
+ @Test(dataProvider = "alternateNamesListArgs")
+ public void testAlternateNamesForListParameters(String[] argv) {
+ AlternateNamesForListArgs args = new AlternateNamesForListArgs();
+
+ new JCommander(args, argv);
+
+ Assert.assertEquals(args.serverNames.size(), 3);
+ Assert.assertEquals(args.serverNames.get(0), argv[1]);
+ Assert.assertEquals(args.serverNames.get(1), argv[3]);
+ Assert.assertEquals(args.serverNames.get(2), argv[5]);
+ }
+
+
+ /**
+ * Make sure that if there are args with multiple names (e.g. "-log" and "-verbose"),
+ * the usage will only display it once.
+ */
+ public void repeatedArgs() {
+ Args1 args = new Args1();
+ String[] argv = {"-log", "2"};
+ JCommander jc = new JCommander(args, argv);
+ Assert.assertEquals(jc.getParameters().size(), 8);
+ }
+
+ /**
+ * Not specifying a required option should throw an exception.
+ */
+ @Test(expectedExceptions = ParameterException.class)
+ public void requiredFields1Fail() {
+ Args1 args = new Args1();
+ String[] argv = {"-debug"};
+ new JCommander(args, argv);
+ }
+
+ /**
+ * Getting the description of a nonexistent command should throw an exception.
+ */
+ @Test(expectedExceptions = ParameterException.class)
+ public void nonexistentCommandShouldThrow() {
+ String[] argv = {};
+ JCommander jc = new JCommander(new Object(), argv);
+ jc.getCommandDescription("foo");
+ }
+
+ /**
+ * Required options with multiple names should work with all names.
+ */
+ private void multipleNames(String option) {
+ Args1 args = new Args1();
+ String[] argv = {option, "2"};
+ new JCommander(args, argv);
+ Assert.assertEquals(args.verbose.intValue(), 2);
+ }
+
+ public void multipleNames1() {
+ multipleNames("-log");
+ }
+
+ public void multipleNames2() {
+ multipleNames("-verbose");
+ }
+
+ private void i18n1(String bundleName, Locale locale, String expectedString) {
+ ResourceBundle bundle = locale != null ? ResourceBundle.getBundle(bundleName, locale)
+ : null;
+
+ ArgsI18N1 i18n = new ArgsI18N1();
+ String[] argv = {"-host", "localhost"};
+ JCommander jc = new JCommander(i18n, bundle, argv);
+
+ ParameterDescription pd = jc.getParameters().get(0);
+ Assert.assertEquals(pd.getDescription(), expectedString);
+ }
+
+ public void i18nNoLocale() {
+ i18n1("MessageBundle", null, "Host");
+ }
+
+ public void i18nUsLocale() {
+ i18n1("MessageBundle", new Locale("en", "US"), "Host");
+ }
+
+ public void i18nFrLocale() {
+ i18n1("MessageBundle", new Locale("fr", "FR"), "Hôte");
+ }
+
+ private void i18n2(Object i18n) {
+ String[] argv = {"-host", "localhost"};
+ Locale.setDefault(new Locale("fr", "FR"));
+ JCommander jc = new JCommander(i18n, argv);
+ ParameterDescription pd = jc.getParameters().get(0);
+ Assert.assertEquals(pd.getDescription(), "Hôte");
+ }
+
+ public void i18nWithResourceAnnotation() {
+ i18n2(new ArgsI18N2());
+ }
+
+ public void i18nWithResourceAnnotationNew() {
+ i18n2(new ArgsI18N2New());
+ }
+
+ public void i18MissingKeyForCommand() {
+ ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", new Locale("en", "US"));
+ JCommander jc = new JCommander(new ArgsHelp(), bundle);
+ jc.addCommand(new ArgsLongCommandDescription());
+ StringBuilder sb = new StringBuilder();
+ jc.usage(sb);
+ String usage = sb.toString();
+ Assert.assertTrue(usage.contains("text"));
+ }
+
+ public void noParseConstructor() {
+ JCommander jCommander = new JCommander(new ArgsMainParameter1());
+ jCommander.usage(new StringBuilder());
+ // Before fix, this parse would throw an exception, because it calls createDescription, which
+ // was already called by usage(), and can only be called once.
+ jCommander.parse();
+ }
+
+ /**
+ * Test a use case where there are required parameters, but you still want
+ * to interrogate the options which are specified.
+ */
+ public void usageWithRequiredArgsAndResourceBundle() {
+ ArgsHelp argsHelp = new ArgsHelp();
+ JCommander jc = new JCommander(new Object[]{argsHelp, new ArgsRequired()},
+ java.util.ResourceBundle.getBundle("MessageBundle"));
+ // Should be able to display usage without triggering validation
+ jc.usage(new StringBuilder());
+ try {
+ jc.parse("-h");
+ Assert.fail("Should have thrown a required parameter exception");
+ } catch (ParameterException e) {
+ Assert.assertTrue(e.getMessage().contains("are required"));
+ }
+ Assert.assertTrue(argsHelp.help);
+ }
+
+ public void multiObjects() {
+ ArgsMaster m = new ArgsMaster();
+ ArgsSlave s = new ArgsSlave();
+ String[] argv = {"-master", "master", "-slave", "slave"};
+ new JCommander(new Object[]{m, s}, argv);
+
+ Assert.assertEquals(m.master, "master");
+ Assert.assertEquals(s.slave, "slave");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void multiObjectsWithDuplicatesFail() {
+ ArgsMaster m = new ArgsMaster();
+ ArgsSlave s = new ArgsSlaveBogus();
+ String[] argv = {"-master", "master", "-slave", "slave"};
+ new JCommander(new Object[]{m, s}, argv);
+ }
+
+ public void arityString() {
+ ArgsArityString args = new ArgsArityString();
+ String[] argv = {"-pairs", "pair0", "pair1", "rest"};
+ new JCommander(args, argv);
+
+ Assert.assertEquals(args.pairs.size(), 2);
+ Assert.assertEquals(args.pairs.get(0), "pair0");
+ Assert.assertEquals(args.pairs.get(1), "pair1");
+ Assert.assertEquals(args.rest.size(), 1);
+ Assert.assertEquals(args.rest.get(0), "rest");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void arity2Fail() {
+ ArgsArityString args = new ArgsArityString();
+ String[] argv = {"-pairs", "pair0"};
+ new JCommander(args, argv);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void multipleUnparsedFail() {
+ ArgsMultipleUnparsed args = new ArgsMultipleUnparsed();
+ String[] argv = {};
+ new JCommander(args, argv);
+ }
+
+ public void privateArgs() {
+ ArgsPrivate args = new ArgsPrivate();
+ new JCommander(args, "-verbose", "3");
+ Assert.assertEquals(args.getVerbose().intValue(), 3);
+ }
+
+ @Test(
+ expectedExceptions = ParameterException.class,
+ expectedExceptionsMessageRegExp = "Cannot use final field .*#_foo as a parameter;"
+ + " compile-time constant inlining may hide new values written to it.")
+ public void finalArgs() {
+ Object args = new Object() {
+ @Parameter(names = "-foo")
+ final int _foo = 0;
+ };
+ new JCommander(args).usage();
+ }
+
+ public void converterArgs() {
+ ArgsConverter args = new ArgsConverter();
+ String fileName = "a";
+ new JCommander(args, "-file", "/tmp/" + fileName,
+ "-listStrings", "Tuesday,Thursday",
+ "-listInts", "-1,8",
+ "-listBigDecimals", "-11.52,100.12");
+ Assert.assertEquals(args.file.getName(), fileName);
+ Assert.assertEquals(args.listStrings.size(), 2);
+ Assert.assertEquals(args.listStrings.get(0), "Tuesday");
+ Assert.assertEquals(args.listStrings.get(1), "Thursday");
+ Assert.assertEquals(args.listInts.size(), 2);
+ Assert.assertEquals(args.listInts.get(0).intValue(), -1);
+ Assert.assertEquals(args.listInts.get(1).intValue(), 8);
+ Assert.assertEquals(args.listBigDecimals.size(), 2);
+ Assert.assertEquals(args.listBigDecimals.get(0), new BigDecimal("-11.52"));
+ Assert.assertEquals(args.listBigDecimals.get(1), new BigDecimal("100.12"));
+ }
+
+ public void hiddenConverter() {
+ class Args {
+ @Parameter(names = "--path", converter = HiddenConverter.class)
+ public String path;
+ }
+
+ new JCommander(new Args(), "--path", "/tmp/a");
+ }
+
+ public void hiddenArgs() {
+ new JCommander(new HiddenArgs(), "--input", "/tmp/a", "--output", "/tmp/b");
+ }
+
+ public void hiddenSplitter() {
+ class Args {
+ @Parameter(names = "--extensions", splitter = HiddenParameterSplitter.class)
+ public List<String> extensions;
+ }
+ if (HiddenParameterSplitter.class.getConstructors().length == 0) {
+ return; // Compiler has optimised away the private constructor
+ }
+
+ Args args = new Args();
+ new JCommander(args, "--extensions", ".txt;.md");
+ Assert.assertEquals(Arrays.asList(".txt", ".md"), args.extensions);
+ }
+
+ private void argsBoolean1(String[] params, Boolean expected) {
+ ArgsBooleanArity args = new ArgsBooleanArity();
+ new JCommander(args, params);
+ Assert.assertEquals(args.debug, expected);
+ }
+
+ private void argsBoolean0(String[] params, Boolean expected) {
+ ArgsBooleanArity0 args = new ArgsBooleanArity0();
+ new JCommander(args, params);
+ Assert.assertEquals(args.debug, expected);
+ }
+
+ public void booleanArity1() {
+ argsBoolean1(new String[]{}, Boolean.FALSE);
+ argsBoolean1(new String[]{"-debug", "true"}, Boolean.TRUE);
+ }
+
+ public void booleanArity0() {
+ argsBoolean0(new String[]{}, Boolean.FALSE);
+ argsBoolean0(new String[]{"-debug"}, Boolean.TRUE);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void badParameterShouldThrowParameter1Exception() {
+ Args1 args = new Args1();
+ String[] argv = {"-log", "foo"};
+ new JCommander(args, argv);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void badParameterShouldThrowParameter2Exception() {
+ Args1 args = new Args1();
+ String[] argv = {"-long", "foo"};
+ new JCommander(args, argv);
+ }
+
+ public void listParameters() {
+ Args2 a = new Args2();
+ String[] argv = {"-log", "2", "-groups", "unit", "a", "b", "c", "-host", "host2"};
+ new JCommander(a, argv);
+ Assert.assertEquals(a.verbose.intValue(), 2);
+ Assert.assertEquals(a.groups, "unit");
+ Assert.assertEquals(a.hosts, Arrays.asList("host2"));
+ Assert.assertEquals(a.parameters, Arrays.asList("a", "b", "c"));
+ }
+
+ public void separatorEqual() {
+ SeparatorEqual s = new SeparatorEqual();
+ String[] argv = {"-log=3", "--longoption=10"};
+ new JCommander(s, argv);
+ Assert.assertEquals(s.log.intValue(), 3);
+ Assert.assertEquals(s.longOption.intValue(), 10);
+ }
+
+ public void separatorColon() {
+ SeparatorColon s = new SeparatorColon();
+ String[] argv = {"-verbose:true"};
+ new JCommander(s, argv);
+ Assert.assertTrue(s.verbose);
+ }
+
+ public void separatorBoth() {
+ SeparatorColon s = new SeparatorColon();
+ SeparatorEqual s2 = new SeparatorEqual();
+ String[] argv = {"-verbose:true", "-log=3"};
+ new JCommander(new Object[]{s, s2}, argv);
+ Assert.assertTrue(s.verbose);
+ Assert.assertEquals(s2.log.intValue(), 3);
+ }
+
+ public void separatorMixed1() {
+ SeparatorMixed s = new SeparatorMixed();
+ String[] argv = {"-long:1", "-level=42"};
+ new JCommander(s, argv);
+ Assert.assertEquals(s.l.longValue(), 1l);
+ Assert.assertEquals(s.level.intValue(), 42);
+ }
+
+ public void slashParameters() {
+ SlashSeparator a = new SlashSeparator();
+ String[] argv = {"/verbose", "/file", "/tmp/a"};
+ new JCommander(a, argv);
+ Assert.assertTrue(a.verbose);
+ Assert.assertEquals(a.file, "/tmp/a");
+ }
+
+ public void inheritance() {
+ ArgsInherited args = new ArgsInherited();
+ String[] argv = {"-log", "3", "-child", "2"};
+ new JCommander(args, argv);
+ Assert.assertEquals(args.child.intValue(), 2);
+ Assert.assertEquals(args.log.intValue(), 3);
+ }
+
+ public void negativeNumber() {
+ Args1 a = new Args1();
+ String[] argv = {"-verbose", "-3"};
+ new JCommander(a, argv);
+ Assert.assertEquals(a.verbose.intValue(), -3);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void requiredMainParameters() {
+ ArgsRequired a = new ArgsRequired();
+ String[] argv = {};
+ new JCommander(a, argv);
+ }
+
+ public void usageShouldNotChange() {
+ JCommander jc = new JCommander(new Args1(), "-log", "1");
+ StringBuilder sb = new StringBuilder();
+ jc.usage(sb);
+ String expected = sb.toString();
+ jc = new JCommander(new Args1(), "-debug", "-log", "2", "-long", "5");
+ sb = new StringBuilder();
+ jc.usage(sb);
+ String actual = sb.toString();
+ Assert.assertEquals(actual, expected);
+ }
+
+ private void verifyCommandOrdering(String[] commandNames, Object[] commands) {
+ CommandMain cm = new CommandMain();
+ JCommander jc = new JCommander(cm);
+
+ for (int i = 0; i < commands.length; i++) {
+ jc.addCommand(commandNames[i], commands[i]);
+ }
+
+ Map<String, JCommander> c = jc.getCommands();
+ Assert.assertEquals(c.size(), commands.length);
+
+ Iterator<String> it = c.keySet().iterator();
+ for (int i = 0; i < commands.length; i++) {
+ Assert.assertEquals(it.next(), commandNames[i]);
+ }
+ }
+
+ public void commandsShouldBeShownInOrderOfInsertion() {
+ verifyCommandOrdering(new String[]{"add", "commit"},
+ new Object[]{new CommandAdd(), new CommandCommit()});
+ verifyCommandOrdering(new String[]{"commit", "add"},
+ new Object[]{new CommandCommit(), new CommandAdd()});
+ }
+
+ @DataProvider
+ public static Object[][] f() {
+ return new Integer[][]{
+ new Integer[]{3, 5, 1},
+ new Integer[]{3, 8, 1},
+ new Integer[]{3, 12, 2},
+ new Integer[]{8, 12, 2},
+ new Integer[]{9, 10, 1},
+ };
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void arity1Fail() {
+ final Arity1 arguments = new Arity1();
+ final JCommander jCommander = new JCommander(arguments);
+ final String[] commands = {
+ "-inspect"
+ };
+ jCommander.parse(commands);
+ }
+
+ public void arity1Success1() {
+ final Arity1 arguments = new Arity1();
+ final JCommander jCommander = new JCommander(arguments);
+ final String[] commands = {
+ "-inspect", "true"
+ };
+ jCommander.parse(commands);
+ Assert.assertTrue(arguments.inspect);
+ }
+
+ public void arity1Success2() {
+ final Arity1 arguments = new Arity1();
+ final JCommander jCommander = new JCommander(arguments);
+ final String[] commands = {
+ "-inspect", "false"
+ };
+ jCommander.parse(commands);
+ Assert.assertFalse(arguments.inspect);
+ }
+
+ @Parameters(commandDescription = "Help for the given commands.")
+ public static class Help {
+ public static final String NAME = "help";
+
+ @Parameter(description = "List of commands.")
+ public List<String> commands = new ArrayList<>();
+ }
+
+ @Test(expectedExceptions = ParameterException.class,
+ description = "Verify that the main parameter's type is checked to be a List")
+ public void wrongMainTypeShouldThrow() {
+ JCommander jc = new JCommander(new ArgsRequiredWrongMain());
+ jc.parse("f1", "f2");
+ }
+
+ @Test(description = "This used to run out of memory")
+ public void oom() {
+ JCommander jc = new JCommander(new ArgsOutOfMemory());
+ jc.usage(new StringBuilder());
+ }
+
+ @Test
+ public void getParametersShouldNotNpe() {
+ JCommander jc = new JCommander(new Args1());
+ List<ParameterDescription> parameters = jc.getParameters();
+ }
+
+ public void validationShouldWork1() {
+ ArgsValidate1 a = new ArgsValidate1();
+ JCommander jc = new JCommander(a);
+ jc.parse("-age", "2 ");
+ Assert.assertEquals(a.age, new Integer(2));
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void validationShouldWorkWithDefaultValues() {
+ ArgsValidate2 a = new ArgsValidate2();
+ new JCommander(a).usage();
+ }
+
+ @Test
+ public void multipleValidators() {
+ for (int i = 1; i < 100; i += 2) {
+ ArgsMultiValidate a = new ArgsMultiValidate();
+ JCommander jc = new JCommander(a);
+ jc.parse("-age", String.valueOf(i));
+ }
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void multipleValidatorsFails1() {
+ ArgsMultiValidate a = new ArgsMultiValidate();
+ JCommander jc = new JCommander(a);
+ jc.parse("-age", "131");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void multipleValidatorsFails2() {
+ ArgsMultiValidate a = new ArgsMultiValidate();
+ JCommander jc = new JCommander(a);
+ jc.parse("-age", "0");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void validationShouldWork2() {
+ ArgsValidate1 a = new ArgsValidate1();
+ JCommander jc = new JCommander(a);
+ jc.parse("-age", "-2 ");
+ }
+
+ @Test
+ public void validationShouldReceiveRightParameterName() {
+ ArgMultiNameValidator validator = new ArgMultiNameValidator();
+ JCommander jc = new JCommander(validator);
+ String paramName = "-name2";
+ jc.parse(paramName, "param1");
+ Assert.assertEquals(ArgMultiNameValidator.MultiNameValidator.parsedName, paramName);
+ }
+
+ public void atFileCanContainEmptyLines() throws IOException {
+ File f = File.createTempFile("JCommander", null);
+ f.deleteOnExit();
+ FileWriter fw = new FileWriter(f);
+ fw.write("-log\n");
+ fw.write("\n");
+ fw.write("2\n");
+ fw.close();
+ new JCommander(new Args1(), "@" + f.getAbsolutePath());
+ }
+
+ public void atFileWithInNonDefaultCharset() throws IOException {
+ final Charset utf32 = Charset.forName("UTF-32");
+ final File f = File.createTempFile("JCommander", null);
+ f.deleteOnExit();
+ try (OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(f), utf32)) {
+ fw.write("-log\n");
+ fw.write("2\n");
+ fw.write("-groups\n");
+ fw.write("\u9731\n");
+ }
+ final Args1 args1 = new Args1();
+ final JCommander jc = new JCommander(args1);
+ try {
+ jc.parse("@" + f.getAbsolutePath());
+ throw new IllegalStateException("Expected exception to be thrown");
+ } catch (ParameterException expected) {
+ Assert.assertTrue(expected.getMessage().startsWith("Could not read file"));
+ }
+ jc.setAtFileCharset(utf32);
+ jc.parse("@" + f.getAbsolutePath());
+ Assert.assertEquals("\u9731", args1.groups);
+ }
+
+ public void handleEqualSigns() {
+ ArgsEquals a = new ArgsEquals();
+ JCommander jc = new JCommander(a);
+ jc.parse("-args=a=b,b=c");
+ Assert.assertEquals(a.args, "a=b,b=c");
+ }
+
+ @SuppressWarnings("serial")
+ public void handleSets() {
+ ArgsWithSet a = new ArgsWithSet();
+ new JCommander(a, "-s", "3,1,2");
+ Assert.assertEquals(a.set, new TreeSet<Integer>() {{
+ add(1);
+ add(2);
+ add(3);
+ }});
+ }
+
+ private static final List<String> V = Arrays.asList("a", "b", "c", "d");
+
+ @DataProvider
+ public Object[][] variable() {
+ return new Object[][]{
+ //new Object[]{0, V.subList(0, 0), V},
+ new Object[]{1, V.subList(0, 1), V.subList(1, 4)},
+ new Object[]{2, V.subList(0, 2), V.subList(2, 4)},
+ new Object[]{3, V.subList(0, 3), V.subList(3, 4)},
+ new Object[]{4, V.subList(0, 4), V.subList(4, 4)},
+ };
+ }
+
+ @Test(dataProvider = "variable")
+ public void variableArity(int count, List<String> var, List<String> main) {
+ VariableArity va = new VariableArity(count);
+ new JCommander(va).parse("-variable", "a", "b", "c", "d");
+ Assert.assertEquals(var, va.var);
+ Assert.assertEquals(main, va.main);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void variableArityZeroNonBoolean() {
+ VariableArity va = new VariableArity(0);
+ new JCommander(va).parse("-variable", "a", "b", "c", "d");
+ }
+
+ public void enumArgs() {
+ ArgsEnum args = new ArgsEnum();
+ String[] argv = {"-choice", "ONE", "-choices", "ONE", "Two"};
+ JCommander jc = new JCommander(args, argv);
+
+ Assert.assertEquals(args.choice, ArgsEnum.ChoiceType.ONE);
+
+ List<ChoiceType> expected = Arrays.asList(ChoiceType.ONE, ChoiceType.Two);
+ Assert.assertEquals(expected, args.choices);
+
+ for (ParameterDescription param : jc.getParameters()) {
+ // order can vary depending on JDK version
+ if (param.getLongestName().equals("-choice")) {
+ Assert.assertEquals(param.getDescription(),
+ "Options: " + EnumSet.allOf((Class<? extends Enum>) ArgsEnum.ChoiceType.class));
+ return;
+ }
+ }
+ Assert.fail("Could not find -choice parameter.");
+ }
+
+ public void enumArgs2() {
+ // issue #266
+ ArgsEnum args = new ArgsEnum();
+ new JCommander(args, "-choices", "ONE,Two");
+ Assert.assertEquals(Arrays.asList(ChoiceType.ONE, ChoiceType.Two), args.choices);
+ }
+
+ public void enumArgsCaseInsensitive() {
+ ArgsEnum args = new ArgsEnum();
+ String[] argv = {"-choice", "one"};
+ JCommander jc = new JCommander(args, argv);
+
+ Assert.assertEquals(args.choice, ArgsEnum.ChoiceType.ONE);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void enumArgsFail() {
+ ArgsEnum args = new ArgsEnum();
+ String[] argv = {"-choice", "A"};
+ new JCommander(args, argv);
+ }
+
+ @Test
+ public void testDefaultListConverterForString() {
+ ArgsList al = new ArgsList();
+ JCommander j = new JCommander(al);
+ j.parse("-groups", "a,b");
+ Assert.assertEquals(al.groups.get(0), "a");
+ Assert.assertEquals(al.groups.get(1), "b");
+ }
+
+ @Test
+ public void testDefaultListConverterForStandardType() {
+ ArgsList al = new ArgsList();
+ JCommander j = new JCommander(al);
+ j.parse("-ints", "41,42");
+ Assert.assertEquals(al.ints.get(0).intValue(), 41);
+ Assert.assertEquals(al.ints.get(1).intValue(), 42);
+ }
+
+ @Test
+ public void testDefaultListConverterWithCustomConverterAndSplitter() {
+ ArgsList al = new ArgsList();
+ JCommander j = new JCommander(al);
+ j.parse("-hp", "localhost:1000;example.com:1001");
+ Assert.assertEquals(al.hostPorts.get(0).host, "localhost");
+ Assert.assertEquals(al.hostPorts.get(0).port.intValue(), 1000);
+ Assert.assertEquals(al.hostPorts.get(1).host, "example.com");
+ Assert.assertEquals(al.hostPorts.get(1).port.intValue(), 1001);
+ }
+
+ @Test
+ public void testDefaultListConverterWithCustomConverterAndDefaultSplitter() {
+ ArgsList al = new ArgsList();
+ JCommander j = new JCommander(al);
+ j.parse("-hp2", "localhost:1000,example.com:1001");
+ Assert.assertEquals(al.hp2.get(1).host, "example.com");
+ Assert.assertEquals(al.hp2.get(1).port.intValue(), 1001);
+ }
+
+ @Test
+ public void testCustomListConverter() {
+ ArgsList al = new ArgsList();
+ JCommander j = new JCommander(al);
+ j.parse("-uppercase", "ab,cd");
+ Assert.assertEquals(al.uppercase.get(0), "AB");
+ Assert.assertEquals(al.uppercase.get(1), "CD");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void shouldThrowIfUnknownOption() {
+ class A {
+ @Parameter(names = "-long")
+ public long l;
+ }
+ A a = new A();
+ new JCommander(a).parse("-lon", "32");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void mainParameterShouldBeValidate() {
+ class V implements IParameterValidator {
+
+ @Override
+ public void validate(String name, String value) throws ParameterException {
+ Assert.assertEquals("a", value);
+ }
+ }
+
+ class A {
+ @Parameter(validateWith = V.class)
+ public List<String> m;
+ }
+
+ A a = new A();
+ new JCommander(a).parse("b");
+ }
+
+ @Parameters(commandNames = {"--configure"})
+ public static class ConfigureArgs {
+ }
+
+ public static class BaseArgs {
+ @Parameter(names = {"-h", "--help"}, description = "Show this help screen")
+ private boolean help = false;
+
+ @Parameter(names = {"--version", "-version"}, description = "Show the program version")
+ private boolean version;
+ }
+
+ public void commandsWithSamePrefixAsOptionsShouldWork() {
+ BaseArgs a = new BaseArgs();
+ ConfigureArgs conf = new ConfigureArgs();
+ JCommander jc = new JCommander(a);
+ jc.addCommand(conf);
+ jc.parse("--configure");
+ }
+
+ public void dynamicParameters() {
+ class Command {
+ @DynamicParameter(names = {"-P"}, description = "Additional command parameters")
+ private Map<String, String> params = Maps.newHashMap();
+ }
+ JCommander commander = new JCommander();
+ Command c = new Command();
+ commander.addCommand("command", c);
+ commander.parse("command", "-Pparam='name=value'");
+ Assert.assertEquals(c.params.get("param"), "'name=value'");
+ }
+
+ public void exeParser() {
+ class Params {
+ @Parameter(names = "-i")
+ private String inputFile;
+ }
+
+ String args[] = {"-i", ""};
+ Params p = new Params();
+ new JCommander(p, args);
+ }
+
+ public void multiVariableArityList() {
+ class Params {
+ @Parameter(names = "-paramA", description = "ParamA", variableArity = true)
+ private List<String> paramA = Lists.newArrayList();
+
+ @Parameter(names = "-paramB", description = "ParamB", variableArity = true)
+ private List<String> paramB = Lists.newArrayList();
+ }
+
+ {
+ String args[] = {"-paramA", "a1", "a2", "-paramB", "b1", "b2", "b3"};
+ Params p = new Params();
+ new JCommander(p, args).parse();
+ Assert.assertEquals(p.paramA, Arrays.asList("a1", "a2"));
+ Assert.assertEquals(p.paramB, Arrays.asList("b1", "b2", "b3"));
+ }
+
+ {
+ String args[] = {"-paramA", "a1", "a2", "-paramB", "b1", "-paramA", "a3"};
+ Params p = new Params();
+ new JCommander(p, args).parse();
+ Assert.assertEquals(p.paramA, Arrays.asList("a1", "a2", "a3"));
+ Assert.assertEquals(p.paramB, Arrays.asList("b1"));
+ }
+ }
+
+ @Test(enabled = false,
+ description = "Need to double check that the command description is i18n'ed in the usage")
+ public void commandKey() {
+ @Parameters(resourceBundle = "MessageBundle", commandDescriptionKey = "command")
+ class Args {
+ @Parameter(names = "-myoption", descriptionKey = "myoption")
+ private boolean option;
+ }
+ JCommander j = new JCommander();
+ Args a = new Args();
+ j.addCommand("comm", a);
+ j.usage();
+ }
+
+ @Test(expectedExceptions = ParameterException.class,
+ expectedExceptionsMessageRegExp = "Was passed main parameter '' but no main parameter was defined.*")
+ public void tmp() {
+ class A {
+ @Parameter(names = "-b")
+ public String b;
+ }
+ new JCommander(new A()).parse("");
+ }
+
+ @Test(expectedExceptions = ParameterException.class, expectedExceptionsMessageRegExp = "\"--b\": couldn't convert \"ThisIsATest\" to an integer")
+ public void multipleParameterNames() {
+ class MultipleParameterNames {
+ @Parameter(names = {"-b", "--b"})
+ public Integer b;
+ }
+ new JCommander(new MultipleParameterNames()).parse("--b", "ThisIsATest");
+ }
+
+ public void unknownOptionWithDifferentPrefix() {
+ @Parameters
+ class SlashSeparator {
+
+ @Parameter(names = "/verbose")
+ public boolean verbose = false;
+
+ @Parameter(names = "/file")
+ public String file;
+ }
+ SlashSeparator ss = new SlashSeparator();
+ try {
+ new JCommander(ss).parse("/notAParam");
+ } catch (ParameterException ex) {
+ boolean result = ex.getMessage().contains("in your arg class");
+ Assert.assertTrue(result);
+ }
+ }
+
+ public void equalSeparator() {
+ @Parameters(separators = "=", commandDescription = "My command")
+ class MyClass {
+
+ @Parameter(names = {"-p", "--param"}, required = true, description = "param desc...")
+ private String param;
+ }
+ MyClass c = new MyClass();
+ String expected = "\"hello\"world";
+ new JCommander(c).parse("--param=" + expected);
+ Assert.assertEquals(expected, c.param);
+ }
+
+ public void simpleArgsSetter() throws ParseException {
+ Args1Setter args = new Args1Setter();
+ String[] argv = {"-debug", "-log", "2", "-float", "1.2", "-double", "1.3", "-bigdecimal", "1.4",
+ "-date", "2011-10-26", "-groups", "unit", "a", "b", "c"};
+ new JCommander(args, argv);
+
+ Assert.assertTrue(args.debug);
+ Assert.assertEquals(args.verbose.intValue(), 2);
+ Assert.assertEquals(args.groups, "unit");
+ Assert.assertEquals(args.parameters, Arrays.asList("a", "b", "c"));
+ Assert.assertEquals(args.floa, 1.2f, 0.1f);
+ Assert.assertEquals(args.doub, 1.3f, 0.1f);
+ Assert.assertEquals(args.bigd, new BigDecimal("1.4"));
+ Assert.assertEquals(args.date, new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-26"));
+ }
+
+ public void verifyHelp() {
+ class Arg {
+ @Parameter(names = "--help", help = true)
+ public boolean help = false;
+
+ @Parameter(names = "file", required = true)
+ public String file;
+ }
+ Arg arg = new Arg();
+ String[] argv = {"--help"};
+ new JCommander(arg, argv);
+
+ Assert.assertTrue(arg.help);
+ }
+
+ public void helpTest() {
+ class Arg {
+ @Parameter(names = {"?", "-help", "--help"}, description = "Shows help", help = true)
+ private boolean help = false;
+ }
+ Arg arg = new Arg();
+ JCommander jc = new JCommander(arg);
+ jc.parse("-help");
+ }
+
+ @Test
+ public void doNotDisplayHelpDefaultValue() {
+ class Arg {
+ @Parameter(names = "--help", help = true)
+ public boolean help = false;
+ }
+ Arg arg = new Arg();
+ String[] argv = {"--help"};
+ JCommander jc = new JCommander(arg, argv);
+
+ StringBuilder sb = new StringBuilder();
+
+ jc.usage(sb);
+
+ Assert.assertFalse(sb.toString().contains("Default"));
+ }
+
+ @Test(enabled = false, description = "Should only be enable once multiple parameters are allowed")
+ public void duplicateParameterNames() {
+ class ArgBase {
+ @Parameter(names = {"-host"})
+ protected String host;
+ }
+
+ class Arg1 extends ArgBase {
+ }
+ Arg1 arg1 = new Arg1();
+
+ class Arg2 extends ArgBase {
+ }
+ Arg2 arg2 = new Arg2();
+
+ JCommander jc = new JCommander(new Object[]{arg1, arg2});
+ jc.parse("-host", "foo");
+ Assert.assertEquals(arg1.host, "foo");
+ Assert.assertEquals(arg2.host, "foo");
+ }
+
+ @Test(enabled = true, description = "Disable top-level @/ampersand file expansion")
+ public void disabledAtSignExpansionTest() {
+ class Params {
+ @Parameter(names = {"-username"})
+ protected String username;
+ }
+
+ Params params = new Params();
+
+ JCommander jc = new JCommander(params);
+ jc.setExpandAtSign(false);
+ jc.parse("-username", "@tzellman");
+ Assert.assertEquals(params.username, "@tzellman");
+ }
+
+ @Test(enabled = true, description = "Enable top-level @/ampersand file expansion, which should throw in this case",
+ expectedExceptions = ParameterException.class)
+ public void enabledAtSignExpansionTest() {
+ class Params {
+ @Parameter(names = {"-username"})
+ protected String username;
+ }
+
+ Params params = new Params();
+
+ JCommander jc = new JCommander(params);
+ jc.parse("-username", "@tzellman");
+ Assert.assertEquals(params.username, "@tzellman");
+ }
+
+ public void parameterWithOneDoubleQuote() {
+ @Parameters(separators = "=")
+ class Arg {
+ @Parameter(names = {"-p", "--param"})
+ private String param;
+ }
+ JCommander jc = new JCommander(new MyClass());
+ jc.parse("-p=\"");
+ }
+
+ public void emptyStringAsDefault() {
+ class Arg {
+ @Parameter(names = "-x")
+ String s = "";
+ }
+ Arg a = new Arg();
+ StringBuilder sb = new StringBuilder();
+ new JCommander(a).usage(sb);
+ Assert.assertTrue(sb.toString().contains("Default: <empty string>"));
+ }
+
+ @Test
+ public void emptyStringShouldBeConsideredAsParameter() {
+ class Arg {
+ @Parameter(description = "parameters")
+ List<String> params;
+ }
+
+ Arg a = new Arg();
+ String[] args = {""};
+
+ new JCommander(a).parse(args);
+ Assert.assertEquals(a.params.size(), 1);
+// Assert.assertEquals();
+ }
+
+ @Test
+ public void doubleQuotedStringShouldBeConsideredAsParameter() {
+ class Arg {
+ @Parameter(description = "parameters")
+ List<String> params;
+ }
+
+ Arg a = new Arg();
+ String[] args = {"\"\""};
+
+ new JCommander(a).parse(args);
+ Assert.assertEquals(a.params.size(), 1);
+// Assert.assertEquals();
+ }
+
+ public void spaces() {
+ class Arg {
+ @Parameter(names = "-rule", description = "rule")
+ private List<String> rules = new ArrayList<>();
+ }
+ Arg a = new Arg();
+ new JCommander(a, "-rule", "some test");
+ Assert.assertEquals(a.rules, Arrays.asList("some test"));
+ }
+
+ static class V2 implements IParameterValidator2 {
+ final static List<String> names = Lists.newArrayList();
+ static boolean validateCalled = false;
+
+ @Override
+ public void validate(String name, String value) throws ParameterException {
+ validateCalled = true;
+ }
+
+ @Override
+ public void validate(String name, String value, ParameterDescription pd)
+ throws ParameterException {
+ names.addAll(Arrays.asList(pd.getParameter().names()));
+ }
+ }
+
+ public void validator2() {
+ class Arg {
+ @Parameter(names = {"-h", "--host"}, validateWith = V2.class)
+ String host;
+ }
+ Arg a = new Arg();
+ V2.names.clear();
+ V2.validateCalled = false;
+ JCommander jc = new JCommander(a, "--host", "h");
+ jc.setAcceptUnknownOptions(true);
+ Assert.assertEquals(V2.names, Arrays.asList("-h", "--host"));
+ Assert.assertTrue(V2.validateCalled);
+ }
+
+ public void usageCommandsUnderUsage() {
+ class Arg {
+ }
+ @Parameters(commandDescription = "command a")
+ class ArgCommandA {
+ @Parameter(description = "command a parameters")
+ List<String> parameters;
+ }
+ @Parameters(commandDescription = "command b")
+ class ArgCommandB {
+ @Parameter(description = "command b parameters")
+ List<String> parameters;
+ }
+
+ Arg a = new Arg();
+
+ JCommander c = new JCommander(a);
+ c.addCommand("a", new ArgCommandA());
+ c.addCommand("b", new ArgCommandB());
+
+ StringBuilder sb = new StringBuilder();
+ c.usage(sb);
+ Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
+ }
+
+ public void usageWithEmpytLine() {
+ class Arg {
+ }
+ @Parameters(commandDescription = "command a")
+ class ArgCommandA {
+ @Parameter(description = "command a parameters")
+ List<String> parameters;
+ }
+ @Parameters(commandDescription = "command b")
+ class ArgCommandB {
+ @Parameter(description = "command b parameters")
+ List<String> parameters;
+ }
+
+ Arg a = new Arg();
+
+ JCommander c = new JCommander(a);
+ c.addCommand("a", new ArgCommandA());
+ c.addCommand("b", new ArgCommandB());
+
+ StringBuilder sb = new StringBuilder();
+ c.usage(sb);
+ Assert.assertTrue(sb.toString().contains("command a parameters\n\n b"));
+ }
+
+ public void usageWithSubCommands() {
+ class Arg {
+ }
+ @Parameters(commandDescription = "command a")
+ class ArgCommandA {
+ @Parameter(description = "command a parameters")
+ List<String> parameters;
+ }
+ @Parameters(commandDescription = "command b")
+ class ArgCommandB {
+ @Parameter(description = "command b parameters")
+ List<String> parameters;
+ }
+
+ Arg a = new Arg();
+
+ JCommander c = new JCommander(a);
+ c.setColumnSize(100);
+ c.addCommand("a", new ArgCommandA());
+
+ // b is a sub-command of a
+ JCommander aCommand = c.getCommands().get("a");
+ aCommand.addCommand("b", new ArgCommandB());
+
+ StringBuilder sb = new StringBuilder();
+ c.usage(sb);
+ Assert.assertTrue(sb.toString().contains("command a parameters\n Commands:"));
+ Assert.assertTrue(sb.toString().contains("command b\n Usage:"));
+ }
+
+ public void partialValidation() {
+ class Arg {
+ @Parameter(names = {"-h", "--host"})
+ String host;
+ }
+ Arg a = new Arg();
+ JCommander jc = new JCommander();
+ jc.setAcceptUnknownOptions(true);
+ jc.addObject(a);
+ jc.parse("-a", "foo", "-h", "host");
+ Assert.assertEquals(a.host, "host");
+ Assert.assertEquals(jc.getUnknownOptions(), Lists.newArrayList("-a", "foo"));
+ }
+
+ /**
+ * GITHUB-137.
+ */
+ public void listArgShouldBeCleared() {
+ class Args {
+ @Parameter(description = "[endpoint]")
+ public List<String> endpoint = Lists.newArrayList("prod");
+ }
+ Args a = new Args();
+ new JCommander(a, "dev");
+ Assert.assertEquals(a.endpoint, Lists.newArrayList("dev"));
+ }
+
+ @Test
+ public void dashDashEmpty() {
+ class Parameters {
+ @Parameter
+ public List<String> mainParameters = new ArrayList<>();
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "--");
+ Assert.assertTrue(a.mainParameters.isEmpty());
+ }
+
+ @Test
+ public void dashDashDashDash() {
+ class Parameters {
+ @Parameter
+ public List<String> mainParameters = new ArrayList<>();
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "--", "--");
+ Assert.assertEquals(a.mainParameters.size(), 1);
+ Assert.assertEquals(a.mainParameters.get(0), "--");
+ }
+
+ public void dashDashParameter() {
+ class Parameters {
+ @Parameter(names = {"-name"})
+ public String name;
+ @Parameter
+ public List<String> mainParameters;
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "-name", "theName", "--", "param1", "param2");
+ Assert.assertEquals(a.name, "theName");
+ Assert.assertEquals(a.mainParameters.size(), 2);
+ Assert.assertEquals(a.mainParameters.get(0), "param1");
+ Assert.assertEquals(a.mainParameters.get(1), "param2");
+ }
+
+ public void dashDashParameter2() {
+ class Parameters {
+ @Parameter(names = {"-name"})
+ public String name;
+ @Parameter
+ public List<String> mainParameters;
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "param1", "param2", "--", "param3", "-name", "theName");
+ Assert.assertNull(a.name);
+ Assert.assertEquals(a.mainParameters.size(), 5);
+ Assert.assertEquals(a.mainParameters.get(0), "param1");
+ Assert.assertEquals(a.mainParameters.get(1), "param2");
+ Assert.assertEquals(a.mainParameters.get(2), "param3");
+ Assert.assertEquals(a.mainParameters.get(3), "-name");
+ Assert.assertEquals(a.mainParameters.get(4), "theName");
+ }
+
+ public void access() {
+ class Parameters {
+ private int bar;
+
+ @Parameter(names = {"-bar", "-foo"})
+ private void setBar(int value) {
+ bar = value;
+ }
+
+ @Parameter(names = "-otherName")
+ private String otherName;
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "-bar", "1");
+ Assert.assertEquals(a.bar, 1);
+ }
+
+ public void noDash() {
+ class Parameters {
+ private int bar;
+
+ @Parameter(names = {"bar", "foo"})
+ private void setBar(int value) {
+ bar = value;
+ }
+
+ @Parameter(names = "otherName")
+ private String otherName;
+ }
+
+ Parameters a = new Parameters();
+ new JCommander(a, "bar", "1");
+ Assert.assertEquals(a.bar, 1);
+ }
+
+ public void commitTest() {
+ CommandCommit cc = new CommandCommit();
+ new JCommander(cc, "--author=cedric");
+ Assert.assertEquals(cc.author, "cedric");
+ }
+
+ static class CommandTemplate {
@Parameter
- public List<String> mainParameters;
- }
-
- Arguments a = new Arguments();
- new JCommander(a, new String[] {
- "param1", "param2", "--", "param3", "-name", "theName"}
- );
- Assert.assertNull(a.name);
- Assert.assertEquals(a.mainParameters.size(), 5);
- Assert.assertEquals(a.mainParameters.get(0), "param1");
- Assert.assertEquals(a.mainParameters.get(1), "param2");
- Assert.assertEquals(a.mainParameters.get(2), "param3");
- Assert.assertEquals(a.mainParameters.get(3), "-name");
- Assert.assertEquals(a.mainParameters.get(4), "theName");
- }
-
- @Test(enabled = false)
- public static void main(String[] args) throws Exception {
- new JCommanderTest().enumArgsFail();
-// class A {
-// @Parameter(names = "-short", required = true)
-// List<String> parameters;
-//
-// @Parameter(names = "-long", required = true)
-// public long l;
-// }
-// A a = new A();
-// new JCommander(a).parse();
-// System.out.println(a.l);
-// System.out.println(a.parameters);
-// ArgsList al = new ArgsList();
-// JCommander j = new JCommander(al);
-// j.setColumnSize(40);
-// j.usage();
-// new JCommanderTest().testListAndSplitters();
-// new JCommanderTest().converterArgs();
- }
-
- // Tests:
- // required unparsed parameter
+ private List<String> parameters = new ArrayList<>();
+
+ @Parameter(names = "help", help = true)
+ private boolean help;
+ }
+
+ public void noDashCommand() {
+ class P1 {
+ @Parameter(names = "hello")
+ private int test;
+ }
+ P1 p1 = new P1();
+ JCommander j = new JCommander();
+ j.addCommand("p1", p1);
+ j.parse("p1", "hello", "47");
+ Assert.assertEquals(p1.test, 47);
+ }
+
+ static class ValuesValidator implements IValueValidator<List<Integer>> {
+ @Override
+ public void validate(String name, List<Integer> values) throws ParameterException {
+ int previous = Integer.MIN_VALUE;
+ for (Integer i : values) {
+ if (i <= previous) {
+ throw new ParameterException("Invalid: values should be strictly increasing.");
+ }
+ previous = i;
+ }
+ }
+ }
+
+ @Test(expectedExceptions = ParameterException.class, expectedExceptionsMessageRegExp = ".*strictly.*")
+ public void issue() {
+ class Parameters {
+ @Parameter(names = {"-v", "--values"},
+ required = true,
+ variableArity = true,
+ validateValueWith = ValuesValidator.class)
+ private List<Integer> values;
+ }
+
+ String[] commands = "-v 1 5 2".split("\\s+");
+ Parameters arg = new Parameters();
+ new JCommander(arg, commands);
+ }
+
+ static class MvParameters {
+ @SubParameter(order = 0)
+ String from;
+ @SubParameter(order = 1)
+ String to;
+ }
+
+ @Test
+ public void arity() {
+ class Parameters {
+ @Parameter(names = {"--mv"}, arity = 2)
+ private MvParameters mvParameters;
+ }
+
+ Parameters args = new Parameters();
+ JCommander.newBuilder()
+ .addObject(args)
+ .args(new String[]{"--mv", "from", "to"})
+ .build();
+
+ Assert.assertNotNull(args.mvParameters);
+ Assert.assertEquals(args.mvParameters.from, "from");
+ Assert.assertEquals(args.mvParameters.to, "to");
+ }
+
+ public void programName() {
+ JCommander jcommander = new JCommander();
+ String programName = "main";
+ jcommander.setProgramName(programName);
+ StringBuilder sb = new StringBuilder();
+ jcommander.usage(sb);
+
+ Assert.assertTrue(sb.toString().contains(programName));
+ Assert.assertEquals(jcommander.getProgramName(), programName);
+ }
+
+ public void dontShowOptionUsageIfThereAreNoOptions() {
+ class CommandTemplate {
+ @Parameter
+ List<String> parameters = new ArrayList<>();
+ }
+
+ CommandTemplate template = new CommandTemplate();
+ JCommander jcommander = new JCommander(template);
+ jcommander.setProgramName("main");
+ StringBuilder sb = new StringBuilder();
+ jcommander.usage(sb);
+ Assert.assertEquals(sb.toString().indexOf("options"), -1);
+ }
+
+ @Test
+ public void annotationsAndDynamicParameters() {
+ class DSimple {
+ @DynamicParameter(names = "-D", description = "Dynamic parameters go here")
+ public Map<String, String> params = Maps.newHashMap();
+
+ @DynamicParameter(names = "-A", assignment = "@")
+ public Map<String, String> params2 = Maps.newHashMap();
+ }
+
+ new JCommander(new DSimple()).usage(new StringBuilder());
+ }
+
+ @Test
+ public void twoCommandsSameOption() {
+ class GenerateOption {
+ @Parameter(names = {"--config"}, required = true, converter = FileConverter.class)
+ public File configFile;
+ }
+
+ class RegenerateOption {
+ @Parameter(names = {"--config"}, required = true, converter = FileConverter.class)
+ public File configFile;
+ }
+
+ GenerateOption generateOption = new GenerateOption();
+ RegenerateOption regenerateOption = new RegenerateOption();
+ JCommander.newBuilder()
+ .addCommand("--generate", generateOption)
+ .addCommand("--regenerate", regenerateOption)
+ .args(new String[]{"--generate", "--config", "foo.txt"})
+ .build();
+ Assert.assertEquals(generateOption.configFile.getName(), "foo.txt");
+ }
+
+ @Test
+ public void invertedBoolean() {
+ class Args {
+ @Parameter(names = {"--f"})
+ private boolean f = true;
+ }
+ Args args = new Args();
+ JCommander.newBuilder()
+ .addObject(args)
+ .args(new String[]{"--f"})
+ .build();
+ Assert.assertEquals(args.f, false);
+ }
+
+ @Test(enabled = false)
+ public static void main(String[] args) {
+
+ CommandTemplate template = new CommandTemplate();
+ JCommander jcommander = new JCommander(template);
+ jcommander.setProgramName("prog");
+ jcommander.parse("help");
+
+ if (template.help) {
+ jcommander.usage();
+ }
+ }
}
diff --git a/src/test/java/com/beust/jcommander/MethodSetterTest.java b/src/test/java/com/beust/jcommander/MethodSetterTest.java
index f995ad6..49bb42c 100644
--- a/src/test/java/com/beust/jcommander/MethodSetterTest.java
+++ b/src/test/java/com/beust/jcommander/MethodSetterTest.java
@@ -26,9 +26,6 @@ public class MethodSetterTest {
public void setRest(List<String> rest) {
this.rest = rest;
}
-// public List<String> getRest() {
-// return this.rest;
-// }
public List<String> rest;
}
ArgsArityStringSetter args = new ArgsArityStringSetter();
@@ -51,7 +48,7 @@ public class MethodSetterTest {
}
boolean passed = false;
try {
- new JCommander(new Arg(), new String[] { "--host", "host" });
+ new JCommander(new Arg(), "--host", "host");
} catch(ParameterException ex) {
Assert.assertEquals(ex.getCause(), null);
passed = true;
@@ -73,7 +70,7 @@ public class MethodSetterTest {
}
}
Arg arg = new Arg();
- new JCommander(arg, new String[] { "--port", "42" });
+ new JCommander(arg, "--port", "42");
Assert.assertEquals(arg.port, new Integer(42));
}
@@ -88,7 +85,7 @@ public class MethodSetterTest {
}
}
Arg arg = new Arg();
- JCommander jc = new JCommander(arg, new String[] { "--port", "42" });
+ JCommander jc = new JCommander(arg, "--port", "42");
ParameterDescription pd = jc.getParameters().get(0);
Assert.assertEquals(pd.getDefault(), 43);
}
diff --git a/src/test/java/com/beust/jcommander/ParametersDelegateTest.java b/src/test/java/com/beust/jcommander/ParametersDelegateTest.java
index 46c7c6a..1f9f9e0 100644
--- a/src/test/java/com/beust/jcommander/ParametersDelegateTest.java
+++ b/src/test/java/com/beust/jcommander/ParametersDelegateTest.java
@@ -143,7 +143,7 @@ public class ParametersDelegateTest {
public void mainParametersTest() {
class Delegate {
@Parameter
- public List<String> mainParams = new ArrayList<String>();
+ public List<String> mainParams = new ArrayList<>();
}
class Command {
@ParametersDelegate
@@ -200,11 +200,11 @@ public class ParametersDelegateTest {
public void duplicateMainParametersAreNotAllowed() {
class Delegate1 {
@Parameter
- public List<String> mainParams1 = new ArrayList<String>();
+ public List<String> mainParams1 = new ArrayList<>();
}
class Delegate2 {
@Parameter
- public List<String> mainParams2 = new ArrayList<String>();
+ public List<String> mainParams2 = new ArrayList<>();
}
class Command {
@ParametersDelegate
diff --git a/src/test/java/com/beust/jcommander/ParametersNotEmptyTest.java b/src/test/java/com/beust/jcommander/ParametersNotEmptyTest.java
new file mode 100644
index 0000000..7ebfc24
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/ParametersNotEmptyTest.java
@@ -0,0 +1,37 @@
+package com.beust.jcommander;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+@Test
+public class ParametersNotEmptyTest {
+
+ public class Args1 {
+ @Parameter(names = "-debug", description = "Debug mode")
+ public boolean debug = false;
+
+ @Parameter(names = "-date", description = "An ISO 8601 formatted date.")
+ public Date date;
+ }
+
+ @Test
+ public void testParameters() throws Exception {
+ JCommander jc = new JCommander(new Args1());
+ List<String> parameters = new ArrayList<>();
+ for (ParameterDescription pd : jc.getParameters()) {
+ parameters.add(pd.getNames());
+ }
+ Collections.sort(parameters);
+
+ Assert.assertEquals(parameters, new ArrayList<String>() {{
+ add("-date");
+ add("-debug");
+ }}
+ );
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/PasswordTest.java b/src/test/java/com/beust/jcommander/PasswordTest.java
new file mode 100644
index 0000000..ff1792e
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/PasswordTest.java
@@ -0,0 +1,111 @@
+package com.beust.jcommander;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+public class PasswordTest {
+
+ @DataProvider(name = "args")
+ public Object[][] createArgs() {
+ return new Object[][] {
+ { new PasswordTestingArgs() },
+ { new OptionalPasswordTestingArgs() },
+ };
+ }
+
+ public interface Args {
+
+ String getPassword();
+
+ int getPort();
+
+ }
+
+ public static class PasswordTestingArgs implements PasswordTest.Args {
+ @Parameter(names = {"--password", "-p"}, description = "Private key password",
+ password = true, required = true)
+ public String password;
+
+ @Parameter(names = {"--port", "-o"}, description = "Port to bind server to",
+ required = true)
+ public int port;
+
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public int getPort() {
+ return port;
+ }
+ }
+
+ @Test(dataProvider = "args")
+ public void passwordNotAsked(Args a) {
+ String expectedPassword = "somepassword";
+ int expectedPort = 7;
+ new JCommander(a, "--password", expectedPassword, "--port", String.valueOf(7));
+ Assert.assertEquals(a.getPort(), expectedPort);
+ Assert.assertEquals(a.getPassword(), expectedPassword);
+ }
+
+ @Test(dataProvider = "args", expectedExceptions = ParameterException.class)
+ public void passwordWithExcessiveArity(Args a) {
+ new JCommander(a, "--password", "somepassword", "someotherarg", "--port", String.valueOf(7));
+ }
+
+ @Test(dataProvider = "args")
+ public void passwordAsked(Args a) {
+ InputStream stdin = System.in;
+ String password = "password";
+ int port = 7;
+ try {
+ System.setIn(new ByteArrayInputStream(password.getBytes()));
+ new JCommander(a, "--port", String.valueOf(port), "--password");
+ Assert.assertEquals(a.getPort(), port);
+ Assert.assertEquals(a.getPassword(), password);
+ } finally {
+ System.setIn(stdin);
+ }
+ }
+
+ public static class OptionalPasswordTestingArgs implements PasswordTest.Args {
+ @Parameter(names = {"--password", "-p"}, description = "Private key password",
+ password = true)
+ public String password;
+
+ @Parameter(names = {"--port", "-o"}, description = "Port to bind server to",
+ required = true)
+ public int port;
+
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public int getPort() {
+ return port;
+ }
+ }
+
+ @Test
+ public void passwordOptionalNotProvided() {
+ Args a = new OptionalPasswordTestingArgs();
+ new JCommander(a, "--port", "7");
+ Assert.assertEquals(a.getPort(), 7);
+ Assert.assertEquals(a.getPassword(), null);
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void passwordRequredNotProvided() {
+ Args a = new PasswordTestingArgs();
+ new JCommander(a, "--port", "7");
+ }
+
+}
diff --git a/src/test/java/com/beust/jcommander/PositiveIntegerTest.java b/src/test/java/com/beust/jcommander/PositiveIntegerTest.java
index ec7d273..81d9b37 100644
--- a/src/test/java/com/beust/jcommander/PositiveIntegerTest.java
+++ b/src/test/java/com/beust/jcommander/PositiveIntegerTest.java
@@ -14,7 +14,7 @@ public class PositiveIntegerTest {
}
Arg arg = new Arg();
JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "-p", "8080" });
+ jc.parse("-p", "8080");
}
@@ -26,7 +26,7 @@ public class PositiveIntegerTest {
}
Arg arg = new Arg();
JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "-p", "" });
+ jc.parse("-p", "");
}
@Test(expectedExceptions = ParameterException.class)
@@ -37,7 +37,7 @@ public class PositiveIntegerTest {
}
Arg arg = new Arg();
JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "-p", "-1" });
+ jc.parse("-p", "-1");
}
@Test(expectedExceptions = ParameterException.class)
@@ -48,7 +48,7 @@ public class PositiveIntegerTest {
}
Arg arg = new Arg();
JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "-p", "abc" });
+ jc.parse("-p", "abc");
}
@Test(expectedExceptions = ParameterException.class)
@@ -60,6 +60,6 @@ public class PositiveIntegerTest {
Arg arg = new Arg();
JCommander jc = new JCommander(arg);
- jc.parse(new String[] { "--port", " " });
+ jc.parse("--port", " ");
}
} \ No newline at end of file
diff --git a/src/test/java/com/beust/jcommander/SetConverter.java b/src/test/java/com/beust/jcommander/SetConverter.java
index c19df11..580fd98 100644
--- a/src/test/java/com/beust/jcommander/SetConverter.java
+++ b/src/test/java/com/beust/jcommander/SetConverter.java
@@ -6,7 +6,7 @@ import java.util.TreeSet;
public class SetConverter implements IStringConverter<SortedSet<Integer>> {
public SortedSet<Integer> convert(String value) {
- SortedSet<Integer> set = new TreeSet<Integer>();
+ SortedSet<Integer> set = new TreeSet<>();
String[] values = value.split(",");
for (String num : values) {
set.add(Integer.parseInt(num));
diff --git a/src/test/java/com/beust/jcommander/SimpleExample.java b/src/test/java/com/beust/jcommander/SimpleExample.java
new file mode 100644
index 0000000..b8000ca
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/SimpleExample.java
@@ -0,0 +1,50 @@
+package com.beust.jcommander;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import static org.testng.Assert.assertEquals;
+
+class Main {
+ static PrintWriter out;
+ @Parameter(names={"--length", "-l"})
+ int length;
+ @Parameter(names={"--pattern", "-p"})
+ int pattern;
+
+ public static void main(String ... args) {
+ Main main = new Main();
+ new JCommander(main, args);
+ main.run();
+ }
+
+ public void run() {
+ out.printf("%d %d", length, pattern);
+ }
+}
+
+public class SimpleExample {
+ StringWriter out;
+
+ @BeforeMethod
+ public void setupMain(){
+ out=new StringWriter();
+ Main.out=new PrintWriter(out);
+ }
+
+ @Test
+ public void testLongArgs() {
+ Main.main("--length", "512", "--pattern", "2");
+ assertEquals("512 2", out.toString());
+ }
+
+ @Test
+ public void testShortArgs() {
+ Main.main("-l", "256", "-p", "171");
+ assertEquals("256 171", out.toString());
+ }
+
+}
diff --git a/src/test/java/com/beust/jcommander/TypeHierarchyTest.java b/src/test/java/com/beust/jcommander/TypeHierarchyTest.java
new file mode 100644
index 0000000..1ae3771
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/TypeHierarchyTest.java
@@ -0,0 +1,111 @@
+package com.beust.jcommander;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * <p>Test that parent classes and interfaces are used correctly</p>
+ */
+public class TypeHierarchyTest {
+
+ public interface Marker {
+ @Parameter(names = {"--available"})
+ void setAvailable(boolean available);
+ }
+
+ public class Base implements Marker {
+ boolean available = false;
+
+ public boolean isAvailable() {
+ return available;
+ }
+
+ @Override
+ public void setAvailable(boolean available) {
+ this.available = true;
+ }
+ }
+
+ public interface IMiddle {
+ @Parameter(names = {"--count"})
+ void setCount(int count);
+ }
+
+ public interface IMiddleMiddle extends IMiddle {
+ @Parameter(names = {"--again"})
+ void setCountAgain(int count);
+ }
+
+ public class Middle extends Base implements IMiddleMiddle {
+
+ private int count;
+ private int again;
+
+ public int getCount() {
+ return count;
+ }
+
+ @Override
+ public void setCount(int count) {
+ this.count = count;
+ }
+
+ public int getCountAgain() {
+ return again;
+ }
+
+ @Override
+ public void setCountAgain(int again) {
+ this.again = again;
+ }
+ }
+
+ // trying to trip it up and get it to go from Child -> Composite -> Visitor
+ public interface Composite extends Visitor {
+ @Parameter(names = {"-n", "--name"})
+ void setName(String validate);
+ }
+
+ public interface Visitor {
+ @Parameter(names = {"--validate"})
+ void setValidate(boolean validate);
+ }
+
+ public class Child extends Middle implements Composite, Visitor{
+
+ private String name;
+ private boolean validate = false;
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean isValidate() {
+ return validate;
+ }
+
+ @Override
+ public void setValidate(boolean validate) {
+ this.validate = validate;
+ }
+ }
+
+ @Test
+ public void testTypeHierarchy() {
+ final Child child = new Child();
+ JCommander commander = new JCommander(child);
+ commander.parse("--validate","-n","child-one","--count","14","--again","22","--available");
+
+ // test values through entire hierarchy
+ Assert.assertTrue(child.isValidate());
+ Assert.assertTrue(child.isAvailable());
+ Assert.assertEquals(14, child.getCount());
+ Assert.assertEquals(22, child.getCountAgain());
+ Assert.assertEquals("child-one", child.getName());
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/ValidatePropertiesWhenParsingTest.java b/src/test/java/com/beust/jcommander/ValidatePropertiesWhenParsingTest.java
index 6a3a98f..e237d73 100644
--- a/src/test/java/com/beust/jcommander/ValidatePropertiesWhenParsingTest.java
+++ b/src/test/java/com/beust/jcommander/ValidatePropertiesWhenParsingTest.java
@@ -10,9 +10,8 @@ public class ValidatePropertiesWhenParsingTest {
JCommander cmd = new JCommander();
cmd.addCommand("a", new A());
-// cmd.addCommand("b", new B());
- cmd.parse(new String[] { "a", "-path", "myPathToHappiness" });
+ cmd.parse("a", "-path", "myPathToHappiness");
}
public static class MyPathValidator implements IParameterValidator {
diff --git a/src/test/java/com/beust/jcommander/VariableArityTest.java b/src/test/java/com/beust/jcommander/VariableArityTest.java
index a90392f..c2d86b8 100644
--- a/src/test/java/com/beust/jcommander/VariableArityTest.java
+++ b/src/test/java/com/beust/jcommander/VariableArityTest.java
@@ -13,15 +13,15 @@ public class VariableArityTest {
@Parameter(names = { "-m", "--matrixData" }, variableArity = true,
description = "File containing a list of instances and their runtimes on various configurations", required = false)
- public List<String> modelMatrixFile = new LinkedList<String>();
+ public List<String> modelMatrixFile = new LinkedList<>();
@Parameter(names = { "-f", "--featureData" }, variableArity = true,
description = "File containing a list of instances and their corresponding features", required = true)
- public List<String> featureFile = new LinkedList<String>();
+ public List<String> featureFile = new LinkedList<>();
@Parameter(names = { "-c", "--configData" }, variableArity = true,
description = "File containing a list of configuration parameter values")
- public List<String> configFile = new LinkedList<String>();
+ public List<String> configFile = new LinkedList<>();
@Parameter(names = { "-o", "--outputFile" },
description = "File to output the resulting data to. Defaults to ./matrix-generation.zip", required = false)
@@ -52,10 +52,9 @@ public class VariableArityTest {
com.parse(split);
-// config.print();
Assert.assertNotEquals(config.seed, 0);
- Assert.assertEquals(config.modelMatrixFile, Arrays.asList(new String[] { "foo" }));
- Assert.assertEquals(config.featureFile, Arrays.asList(new String[] { "foo" }));
+ Assert.assertEquals(config.modelMatrixFile, Arrays.asList("foo"));
+ Assert.assertEquals(config.featureFile, Arrays.asList("foo"));
Assert.assertEquals(config.seed, 1024);
Assert.assertEquals(config.outputFile, "foo");
}
diff --git a/src/test/java/com/beust/jcommander/args/ArgsEnum.java b/src/test/java/com/beust/jcommander/args/ArgsEnum.java
index bef663b..ba0628d 100644
--- a/src/test/java/com/beust/jcommander/args/ArgsEnum.java
+++ b/src/test/java/com/beust/jcommander/args/ArgsEnum.java
@@ -34,12 +34,12 @@ import com.beust.jcommander.Parameter;
*/
public class ArgsEnum {
- public enum ChoiceType { ONE, Two, THREE };
+ public enum ChoiceType { ONE, Two, THREE }
@Parameter(names = "-choice")
public ChoiceType choice = ChoiceType.ONE;
@Parameter(names = "-choices", variableArity = true)
- public List<ChoiceType> choices = new ArrayList<ChoiceType>();
+ public List<ChoiceType> choices = new ArrayList<>();
public static void main(String[] args1) {
ArgsEnum args = new ArgsEnum();
diff --git a/src/test/java/com/beust/jcommander/args/ArgsLongCommandDescription.java b/src/test/java/com/beust/jcommander/args/ArgsLongCommandDescription.java
new file mode 100644
index 0000000..398e514
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/ArgsLongCommandDescription.java
@@ -0,0 +1,12 @@
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+
+@Parameters(commandNames = {"command"}, commandDescription = "text text text text text " +
+ "text text text text text text text text text text text text text text text " +
+ "really-really-really-long-word-or-url text text text text text text text.")
+public class ArgsLongCommandDescription {
+ @Parameter(names = {"-b"}, description = "boolean parameter")
+ public boolean var;
+}
diff --git a/src/test/java/com/beust/jcommander/args/ArgsLongMainParameterDescription.java b/src/test/java/com/beust/jcommander/args/ArgsLongMainParameterDescription.java
new file mode 100644
index 0000000..18bb06c
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/ArgsLongMainParameterDescription.java
@@ -0,0 +1,17 @@
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.Parameter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ArgsLongMainParameterDescription {
+
+ @Parameter(description = "[text] [text] text text text text text text text text " +
+ "text text text text text text text text " +
+ "really-really-really-long-word-or-url text text text text text text text.")
+ public List<String> main = new ArrayList<>();
+
+ @Parameter(names = {"-b"}, description = "boolean parameter")
+ public boolean var;
+}
diff --git a/src/test/java/com/beust/jcommander/args/ArgsMultiValidate.java b/src/test/java/com/beust/jcommander/args/ArgsMultiValidate.java
new file mode 100644
index 0000000..1a913ff
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/ArgsMultiValidate.java
@@ -0,0 +1,36 @@
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.IParameterValidator;
+import com.beust.jcommander.IValueValidator;
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.ParameterException;
+import com.beust.jcommander.validators.PositiveInteger;
+
+public class ArgsMultiValidate {
+
+ public static class OddIntegerParameterValidator implements IParameterValidator {
+ @Override
+ public void validate(String name, String value) throws ParameterException {
+ if(Integer.parseInt(value) %2 != 1) throw new ParameterException("param "+name+"="+value+" is not odd");
+ }
+ }
+
+ public static class LowerThan100ValueValidator implements IValueValidator<Integer> {
+ @Override
+ public void validate(String name, Integer value) throws ParameterException {
+ if(value >= 100) throw new ParameterException("param "+name+"="+value+" is greater than 100");
+ }
+ }
+
+ public static class GreaterTha0ValueValidator implements IValueValidator<Integer> {
+ @Override
+ public void validate(String name, Integer value) throws ParameterException {
+ if(value <= 0) throw new ParameterException("param "+name+"="+value+" is lower than 1");
+ }
+ }
+
+ @Parameter(names = "-age",
+ validateWith = {PositiveInteger.class,OddIntegerParameterValidator.class},
+ validateValueWith={GreaterTha0ValueValidator.class,LowerThan100ValueValidator.class})
+ public int age=29;
+}
diff --git a/src/test/java/com/beust/jcommander/args/HiddenArgs.java b/src/test/java/com/beust/jcommander/args/HiddenArgs.java
new file mode 100644
index 0000000..1b89334
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/HiddenArgs.java
@@ -0,0 +1,36 @@
+/**
+ * Copyright (C) 2010 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.Parameter;
+
+public class HiddenArgs {
+ public HiddenArgs() {
+ }
+
+ @Parameter(names = "--input")
+ private String input;
+
+ private String output;
+
+ @Parameter(names = "--output")
+ private void setOutput(String output) {
+ this.output = output;
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/args/SlashSeparator.java b/src/test/java/com/beust/jcommander/args/SlashSeparator.java
index 64d3930..06b38e5 100644
--- a/src/test/java/com/beust/jcommander/args/SlashSeparator.java
+++ b/src/test/java/com/beust/jcommander/args/SlashSeparator.java
@@ -21,7 +21,7 @@ package com.beust.jcommander.args;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
-@Parameters(optionPrefixes = "/")
+@Parameters
public class SlashSeparator {
@Parameter(names = "/verbose")
diff --git a/src/test/java/com/beust/jcommander/args/VariableArity.java b/src/test/java/com/beust/jcommander/args/VariableArity.java
index 21a861d..ec4130e 100644
--- a/src/test/java/com/beust/jcommander/args/VariableArity.java
+++ b/src/test/java/com/beust/jcommander/args/VariableArity.java
@@ -8,19 +8,19 @@ import java.util.List;
public class VariableArity implements IVariableArity {
- private int m_count;
+ private int count;
public VariableArity(int count) {
- m_count = count;
+ this.count = count;
}
@Parameter
- public List<String> main = new ArrayList<String>();
+ public List<String> main = new ArrayList<>();
@Parameter(names = "-variable", variableArity = true)
- public List<String> var = new ArrayList<String>();
+ public List<String> var = new ArrayList<>();
public int processVariableArity(String optionName, String[] options) {
- return m_count;
+ return count;
}
}
diff --git a/src/test/java/com/beust/jcommander/command/CommandNoParametersAnnotation.java b/src/test/java/com/beust/jcommander/command/CommandNoParametersAnnotation.java
new file mode 100644
index 0000000..8b5c577
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/command/CommandNoParametersAnnotation.java
@@ -0,0 +1,9 @@
+package com.beust.jcommander.command;
+
+import com.beust.jcommander.Parameter;
+import java.util.List;
+
+public class CommandNoParametersAnnotation {
+ @Parameter(description = "Patterns of files to be added")
+ public List<String> patterns;
+}
diff --git a/src/test/java/com/beust/jcommander/command/CommandTest.java b/src/test/java/com/beust/jcommander/command/CommandTest.java
index cf921bd..5cb7c34 100644
--- a/src/test/java/com/beust/jcommander/command/CommandTest.java
+++ b/src/test/java/com/beust/jcommander/command/CommandTest.java
@@ -18,12 +18,13 @@
package com.beust.jcommander.command;
+import com.beust.jcommander.ArgsValidate2;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
-
import org.testng.Assert;
import org.testng.annotations.Test;
+import java.io.File;
import java.util.Arrays;
public class CommandTest {
@@ -75,11 +76,6 @@ public class CommandTest {
jc.addCommand("commit", commit);
jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java");
-// jc.setProgramName("TestCommander");
-// jc.usage();
-// jc.usage("add");
-// jc.usage("commit");
-
Assert.assertTrue(cm.verbose);
Assert.assertEquals(jc.getParsedCommand(), "commit");
Assert.assertTrue(commit.amend);
@@ -109,6 +105,55 @@ public class CommandTest {
Assert.assertFalse(out.toString().contains("hidden Hidden command to add file contents to the index"));
}
+ @Test
+ public void noParametersAnnotationOnCommandTest() {
+ CommandMain cm = new CommandMain();
+ JCommander jc = new JCommander(cm);
+ CommandNoParametersAnnotation noParametersAnnotation = new CommandNoParametersAnnotation();
+ jc.addCommand("no-annotation", noParametersAnnotation);
+
+ jc.setProgramName("TestCommander");
+ StringBuilder out = new StringBuilder();
+ jc.usage(out);
+
+ Assert.assertTrue(out.toString().contains("no-annotation"));
+ }
+
+ @Test
+ public void noTrailingSpaceInUsageTest() {
+ CommandMain cm = new CommandMain();
+ JCommander jc = new JCommander(cm);
+ CommandAdd add = new CommandAdd();
+ jc.addCommand("add", add);
+ CommandCommit commit = new CommandCommit();
+ jc.addCommand("commit", commit);
+ jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java");
+ StringBuilder out = new StringBuilder();
+ jc.usage(out);
+ String firstLine = out.toString().split("\n")[0];
+ Assert.assertFalse(firstLine.endsWith(" "), "Usage should not have trailing spaces");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void validateSubCommand() throws Exception {
+ JCommander jc = new JCommander(new CommandMain());
+ final ArgsValidate2 sub = new ArgsValidate2();
+ sub.template = null;
+ jc.addCommand("sub", sub);
+ jc.parse("sub", "-template", "foo");
+ }
+
+ @Test
+ public void doNotValidateSubCommand() throws Exception {
+ JCommander jc = new JCommander(new CommandMain());
+ final ArgsValidate2 sub = new ArgsValidate2();
+ sub.template = null;
+ jc.addCommand("sub", sub);
+ jc.parseWithoutValidation("sub", "-template", "foo");
+ Assert.assertEquals(sub.template, new File("foo"));
+
+ }
+
public static void main(String[] args) {
new CommandTest().shouldComplainIfNoAnnotations();
}
diff --git a/src/test/java/com/beust/jcommander/converters/CharArrayConverterTest.java b/src/test/java/com/beust/jcommander/converters/CharArrayConverterTest.java
new file mode 100644
index 0000000..596c806
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/converters/CharArrayConverterTest.java
@@ -0,0 +1,37 @@
+/**
+ * Copyright (C) 2010 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.beust.jcommander.converters;
+
+import java.net.UnknownHostException;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test
+public class CharArrayConverterTest {
+
+ private static final String FIXTURE = "Hello World";
+ private static final CharArrayConverter CONVERTER = new CharArrayConverter();
+
+ @Test
+ public void testString() throws UnknownHostException {
+ Assert.assertEquals(CONVERTER.convert(FIXTURE), FIXTURE.toCharArray());
+ }
+
+}
diff --git a/src/test/java/com/beust/jcommander/converters/InetAddressConverterTest.java b/src/test/java/com/beust/jcommander/converters/InetAddressConverterTest.java
new file mode 100644
index 0000000..d8cf8b4
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/converters/InetAddressConverterTest.java
@@ -0,0 +1,62 @@
+/**
+ * Copyright (C) 2010 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.beust.jcommander.converters;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test
+public class InetAddressConverterTest {
+
+ private static final InetAddressConverter INET_ADDRESS_CONVERTER = new InetAddressConverter();
+ private static final InetAddress LOOPBACK_ADDRESS = InetAddress.getLoopbackAddress();
+
+ @Test
+ public void testLocalhost() throws UnknownHostException {
+ test("localhost");
+ }
+
+ @Test
+ public void testLocalhostAddress() throws UnknownHostException {
+ test("127.0.0.1");
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGargabeInput() throws UnknownHostException {
+ test("!@#$%");
+ }
+
+ @Test
+ public void testEmptyInput() throws UnknownHostException {
+ test("");
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testBlankInput() throws UnknownHostException {
+ test(" ");
+ }
+
+ private void test(String string) throws UnknownHostException {
+ Assert.assertEquals(INET_ADDRESS_CONVERTER.convert(string), LOOPBACK_ADDRESS);
+
+ }
+}
diff --git a/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java b/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java
index 98327bd..73e3c9e 100644
--- a/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java
+++ b/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java
@@ -51,10 +51,5 @@ public class DynamicParameterTest {
public static void main(String[] args) {
DynamicParameterTest dpt = new DynamicParameterTest();
dpt.simpleWithSpaces();
-// dpt.nonMapShouldThrow();
-// dpt.wrongSeparatorShouldThrow();
-// dpt.differentAssignment();
-// dpt.arity0();
-// dpt.usage();
}
}