Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unified the uses of spaces vs. tabs

Two source files still included tabs instead of four spaces per indention
  • Loading branch information...
commit 2035bc5f5ba1bfed57c33457d88a8505b9ef456f 1 parent b3371be
penSec.IT UG (haftungsbeschränkt) authored
40 src/jargs/examples/gnu/AutoHelpParser.java
View
@@ -42,37 +42,37 @@
* This example shows how to dynamically create basic output for a --help option.
*/
public class AutoHelpParser extends CmdLineParser {
- List optionHelpStrings = new ArrayList();
+ List optionHelpStrings = new ArrayList();
- public Option addHelp(Option option, String helpString) {
- optionHelpStrings.add(" -" + option.shortForm() + "/--" + option.longForm() + ": " + helpString);
- return option;
- }
-
- public void printUsage() {
+ public Option addHelp(Option option, String helpString) {
+ optionHelpStrings.add(" -" + option.shortForm() + "/--" + option.longForm() + ": " + helpString);
+ return option;
+ }
+
+ public void printUsage() {
System.err.println("usage: prog [options]");
for (Iterator i = optionHelpStrings.iterator(); i.hasNext(); ) {
- System.err.println(i.next());
+ System.err.println(i.next());
}
}
public static void main( String[] args ) {
- AutoHelpParser parser = new AutoHelpParser();
- CmdLineParser.Option verbose = parser.addHelp(
- parser.addBooleanOption('v', "verbose"),
- "Print extra information");
+ AutoHelpParser parser = new AutoHelpParser();
+ CmdLineParser.Option verbose = parser.addHelp(
+ parser.addBooleanOption('v', "verbose"),
+ "Print extra information");
CmdLineParser.Option size = parser.addHelp(
- parser.addIntegerOption('s', "size"),
- "The extent of the thing");
+ parser.addIntegerOption('s', "size"),
+ "The extent of the thing");
CmdLineParser.Option name = parser.addHelp(
- parser.addStringOption('n', "name"),
- "Name given to the widget");
+ parser.addStringOption('n', "name"),
+ "Name given to the widget");
CmdLineParser.Option fraction = parser.addHelp(
- parser.addDoubleOption('f', "fraction"),
- "What percentage should be discarded");
+ parser.addDoubleOption('f', "fraction"),
+ "What percentage should be discarded");
CmdLineParser.Option help = parser.addHelp(
- parser.addBooleanOption('h', "help"),
- "Show this help message");
+ parser.addBooleanOption('h', "help"),
+ "Show this help message");
try {
parser.parse(args);
310 src/jargs/test/gnu/CmdLineParserTestCase.java
View
@@ -42,90 +42,90 @@
public class CmdLineParserTestCase extends TestCase {
- public CmdLineParserTestCase(String name) {
- super(name);
- }
-
- public void testStandardOptions() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- CmdLineParser.Option size = parser.addIntegerOption('s', "size");
- CmdLineParser.Option name = parser.addStringOption('n', "name");
- CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
- CmdLineParser.Option missing = parser.addBooleanOption('m', "missing");
- CmdLineParser.Option careful = parser.addBooleanOption("careful");
- CmdLineParser.Option bignum = parser.addLongOption('b', "bignum");
- assertEquals(null, parser.getOptionValue(size));
- Long longValue = new Long(new Long(Integer.MAX_VALUE).longValue() + 1);
- parser.parse(new String[] { "-v", "--size=100", "-b",
- longValue.toString(), "-n", "foo", "-f", "0.1", "rest" },
- Locale.US);
- assertEquals(null, parser.getOptionValue(missing));
- assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
- assertEquals(100, ((Integer) parser.getOptionValue(size)).intValue());
- assertEquals("foo", parser.getOptionValue(name));
- assertEquals(longValue, parser.getOptionValue(bignum));
- assertEquals(0.1, ((Double) parser.getOptionValue(fraction))
- .doubleValue(), 0.1e-6);
- assertArrayEquals(new String[]{"rest"}, parser.getRemainingArgs());
- }
+ public CmdLineParserTestCase(String name) {
+ super(name);
+ }
+
+ public void testStandardOptions() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option size = parser.addIntegerOption('s', "size");
+ CmdLineParser.Option name = parser.addStringOption('n', "name");
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ CmdLineParser.Option missing = parser.addBooleanOption('m', "missing");
+ CmdLineParser.Option careful = parser.addBooleanOption("careful");
+ CmdLineParser.Option bignum = parser.addLongOption('b', "bignum");
+ assertEquals(null, parser.getOptionValue(size));
+ Long longValue = new Long(new Long(Integer.MAX_VALUE).longValue() + 1);
+ parser.parse(new String[] { "-v", "--size=100", "-b",
+ longValue.toString(), "-n", "foo", "-f", "0.1", "rest" },
+ Locale.US);
+ assertEquals(null, parser.getOptionValue(missing));
+ assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
+ assertEquals(100, ((Integer) parser.getOptionValue(size)).intValue());
+ assertEquals("foo", parser.getOptionValue(name));
+ assertEquals(longValue, parser.getOptionValue(bignum));
+ assertEquals(0.1, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ assertArrayEquals(new String[]{"rest"}, parser.getRemainingArgs());
+ }
public void testDefaults() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option boolean1 = parser.addBooleanOption("boolean1");
- CmdLineParser.Option boolean2 = parser.addBooleanOption("boolean2");
- CmdLineParser.Option boolean3 = parser.addBooleanOption("boolean3");
- CmdLineParser.Option boolean4 = parser.addBooleanOption("boolean4");
- CmdLineParser.Option boolean5 = parser.addBooleanOption("boolean5");
-
- CmdLineParser.Option int1 = parser.addIntegerOption("int1");
- CmdLineParser.Option int2 = parser.addIntegerOption("int2");
- CmdLineParser.Option int3 = parser.addIntegerOption("int3");
- CmdLineParser.Option int4 = parser.addIntegerOption("int4");
-
- CmdLineParser.Option string1 = parser.addStringOption("string1");
- CmdLineParser.Option string2 = parser.addStringOption("string2");
- CmdLineParser.Option string3 = parser.addStringOption("string3");
- CmdLineParser.Option string4 = parser.addStringOption("string4");
-
- parser.parse(new String[] {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option boolean1 = parser.addBooleanOption("boolean1");
+ CmdLineParser.Option boolean2 = parser.addBooleanOption("boolean2");
+ CmdLineParser.Option boolean3 = parser.addBooleanOption("boolean3");
+ CmdLineParser.Option boolean4 = parser.addBooleanOption("boolean4");
+ CmdLineParser.Option boolean5 = parser.addBooleanOption("boolean5");
+
+ CmdLineParser.Option int1 = parser.addIntegerOption("int1");
+ CmdLineParser.Option int2 = parser.addIntegerOption("int2");
+ CmdLineParser.Option int3 = parser.addIntegerOption("int3");
+ CmdLineParser.Option int4 = parser.addIntegerOption("int4");
+
+ CmdLineParser.Option string1 = parser.addStringOption("string1");
+ CmdLineParser.Option string2 = parser.addStringOption("string2");
+ CmdLineParser.Option string3 = parser.addStringOption("string3");
+ CmdLineParser.Option string4 = parser.addStringOption("string4");
+
+ parser.parse(new String[] {
"--boolean1", "--boolean2",
"--int1=42", "--int2=42",
"--string1=Hello", "--string2=Hello",
});
- assertEquals(Boolean.TRUE, parser.getOptionValue(boolean1));
- assertEquals(Boolean.TRUE,
+ assertEquals(Boolean.TRUE, parser.getOptionValue(boolean1));
+ assertEquals(Boolean.TRUE,
parser.getOptionValue(boolean2, Boolean.FALSE));
- assertEquals(null, parser.getOptionValue(boolean3));
- assertEquals(Boolean.FALSE,
+ assertEquals(null, parser.getOptionValue(boolean3));
+ assertEquals(Boolean.FALSE,
parser.getOptionValue(boolean4, Boolean.FALSE));
- assertEquals(Boolean.TRUE,
+ assertEquals(Boolean.TRUE,
parser.getOptionValue(boolean5, Boolean.TRUE));
Integer forty_two = new Integer(42);
Integer thirty_six = new Integer(36);
- assertEquals(forty_two, parser.getOptionValue(int1));
- assertEquals(forty_two, parser.getOptionValue(int2, thirty_six));
- assertEquals(null, parser.getOptionValue(int3));
- assertEquals(thirty_six, parser.getOptionValue(int4, thirty_six));
+ assertEquals(forty_two, parser.getOptionValue(int1));
+ assertEquals(forty_two, parser.getOptionValue(int2, thirty_six));
+ assertEquals(null, parser.getOptionValue(int3));
+ assertEquals(thirty_six, parser.getOptionValue(int4, thirty_six));
- assertEquals("Hello", parser.getOptionValue(string1));
- assertEquals("Hello", parser.getOptionValue(string2, "Goodbye"));
- assertEquals(null, parser.getOptionValue(string3));
- assertEquals("Goodbye", parser.getOptionValue(string4, "Goodbye"));
+ assertEquals("Hello", parser.getOptionValue(string1));
+ assertEquals("Hello", parser.getOptionValue(string2, "Goodbye"));
+ assertEquals(null, parser.getOptionValue(string3));
+ assertEquals("Goodbye", parser.getOptionValue(string4, "Goodbye"));
}
- public void testMultipleUses() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
- CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
+ public void testMultipleUses() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
+ CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
- parser.parse(new String[] {
+ parser.parse(new String[] {
"--foo", "-v", "-v", "--verbose", "-v", "-b", "rest"
});
@@ -150,12 +150,12 @@ public void testMultipleUses() throws Exception {
}
- public void testCombinedFlags() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
- CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- parser.parse(new String[] {
+ public void testCombinedFlags() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
+ CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ parser.parse(new String[] {
"-dv"
});
@@ -165,13 +165,13 @@ public void testCombinedFlags() throws Exception {
}
- public void testExplictlyTerminatedOptions() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
- CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
- parser.parse(new String[] {
+ public void testExplictlyTerminatedOptions() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
+ CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ parser.parse(new String[] {
"-a", "hello", "-d", "-f", "10", "--", "goodbye", "-v", "welcome",
"-f", "-10"
});
@@ -181,19 +181,19 @@ public void testExplictlyTerminatedOptions() throws Exception {
assertEquals(null, parser.getOptionValue(verbose));
assertEquals(new Double(10), parser.getOptionValue(fraction));
- assertArrayEquals(
+ assertArrayEquals(
new String[]{"hello", "goodbye", "-v", "welcome", "-f", "-10"},
parser.getRemainingArgs());
}
- public void testGetOptionValues() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
- CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
+ public void testGetOptionValues() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
+ CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
- parser.parse(new String[] {
+ parser.parse(new String[] {
"--foo", "-v", "-v", "--verbose", "-v", "-b", "rest"
});
@@ -215,76 +215,76 @@ public void testGetOptionValues() throws Exception {
}
- public void testBadFormat() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option size = parser.addIntegerOption('s', "size");
- try {
- parser.parse(new String[] { "--size=blah" });
- fail("Expected IllegalOptionValueException");
- } catch (CmdLineParser.IllegalOptionValueException e) {
- // pass
- }
- }
-
- public void testResetBetweenParse() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
- parser.parse(new String[] { "-v" });
- assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
- parser.parse(new String[] {});
- assertEquals(null, parser.getOptionValue(verbose));
- }
-
- public void testLocale() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
- parser.parse(new String[] { "--fraction=0.2" }, Locale.US);
- assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
- .doubleValue(), 0.1e-6);
- parser.parse(new String[] { "--fraction=0,2" }, Locale.GERMANY);
- assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
- .doubleValue(), 0.1e-6);
- }
-
- public void testDetachedOption() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option detached = new CmdLineParser.Option.BooleanOption(
- 'v', "verbose");
- assertEquals(null, parser.getOptionValue(detached));
- try {
- parser.parse(new String[] { "-v" });
- fail("UnknownOptionException expected");
- } catch (CmdLineParser.UnknownOptionException e) {
- // pass
- }
- assertEquals(null, parser.getOptionValue(detached));
- }
-
- public void testMissingValueForStringOption() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- parser.addBooleanOption('v', "verbose");
- parser.addStringOption('c', "config");
-
- try {
- parser.parse(new String[] {"-v", "-c"});
- fail();
- } catch (CmdLineParser.IllegalOptionValueException e) {
- }
- }
-
- public void testWhitespaceValueForStringOption() throws Exception {
- CmdLineParser parser = new CmdLineParser();
- CmdLineParser.Option opt = parser.addStringOption('o', "option");
- parser.parse(new String[] {"-o", " "});
- assertEquals(" ", parser.getOptionValue(opt));
- }
-
- private void assertArrayEquals(Object[] expected, Object[] actual) {
- assertNotNull(actual);
- assertEquals(expected.length, actual.length);
- for (int i = 0; i < expected.length; ++i) {
- assertEquals(expected[i], actual[i]);
- }
- }
+ public void testBadFormat() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option size = parser.addIntegerOption('s', "size");
+ try {
+ parser.parse(new String[] { "--size=blah" });
+ fail("Expected IllegalOptionValueException");
+ } catch (CmdLineParser.IllegalOptionValueException e) {
+ // pass
+ }
+ }
+
+ public void testResetBetweenParse() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ parser.parse(new String[] { "-v" });
+ assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
+ parser.parse(new String[] {});
+ assertEquals(null, parser.getOptionValue(verbose));
+ }
+
+ public void testLocale() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ parser.parse(new String[] { "--fraction=0.2" }, Locale.US);
+ assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ parser.parse(new String[] { "--fraction=0,2" }, Locale.GERMANY);
+ assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ }
+
+ public void testDetachedOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option detached = new CmdLineParser.Option.BooleanOption(
+ 'v', "verbose");
+ assertEquals(null, parser.getOptionValue(detached));
+ try {
+ parser.parse(new String[] { "-v" });
+ fail("UnknownOptionException expected");
+ } catch (CmdLineParser.UnknownOptionException e) {
+ // pass
+ }
+ assertEquals(null, parser.getOptionValue(detached));
+ }
+
+ public void testMissingValueForStringOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ parser.addBooleanOption('v', "verbose");
+ parser.addStringOption('c', "config");
+
+ try {
+ parser.parse(new String[] {"-v", "-c"});
+ fail();
+ } catch (CmdLineParser.IllegalOptionValueException e) {
+ }
+ }
+
+ public void testWhitespaceValueForStringOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option opt = parser.addStringOption('o', "option");
+ parser.parse(new String[] {"-o", " "});
+ assertEquals(" ", parser.getOptionValue(opt));
+ }
+
+ private void assertArrayEquals(Object[] expected, Object[] actual) {
+ assertNotNull(actual);
+ assertEquals(expected.length, actual.length);
+ for (int i = 0; i < expected.length; ++i) {
+ assertEquals(expected[i], actual[i]);
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.