Search in sources :

Example 1 with Level

use of joptsimple.examples.Level in project jopt-simple by jopt-simple.

the class DefaultValuesForOptionArgumentsTest method allowsSpecificationOfDefaultValues.

@Test
public void allowsSpecificationOfDefaultValues() throws Exception {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File tempFile = File.createTempFile("aFile", ".txt");
    OptionParser parser = new OptionParser();
    OptionSpec<File> infile = parser.accepts("infile").withRequiredArg().ofType(File.class).defaultsTo(tempFile);
    OptionSpec<File> outdir = parser.accepts("outdir").withRequiredArg().ofType(File.class).defaultsTo(tempDir);
    OptionSpec<Integer> bufferSize = parser.accepts("buffer-size").withOptionalArg().ofType(Integer.class).defaultsTo(4096);
    OptionSpec<Level> level = parser.accepts("level").withOptionalArg().ofType(Level.class).defaultsTo(INFO);
    OptionSpec<Integer> count = parser.accepts("count").withOptionalArg().ofType(Integer.class).defaultsTo(10);
    OptionSet options = parser.parse("--level", "WARNING", "--count", "--infile", "/etc/passwd");
    assertEquals(new File("/etc/passwd"), infile.value(options));
    assertTrue(options.has(infile));
    assertTrue(options.hasArgument(infile));
    assertEquals(tempDir, outdir.value(options));
    assertFalse(options.has(outdir));
    assertFalse(options.hasArgument(outdir));
    assertEquals(Integer.valueOf(4096), bufferSize.value(options));
    assertFalse(options.has(bufferSize));
    assertFalse(options.hasArgument(bufferSize));
    assertEquals(WARNING, level.value(options));
    assertTrue(options.has(level));
    assertTrue(options.hasArgument(level));
    assertEquals(Integer.valueOf(10), count.value(options));
    assertTrue(options.has(count));
    assertFalse(options.hasArgument(count));
    thrown.expect(OptionException.class);
    parser.parse("--outdir");
}
Also used : Level(joptsimple.examples.Level) OptionSet(joptsimple.OptionSet) File(java.io.File) OptionParser(joptsimple.OptionParser) Test(org.junit.Test)

Aggregations

File (java.io.File)1 OptionParser (joptsimple.OptionParser)1 OptionSet (joptsimple.OptionSet)1 Level (joptsimple.examples.Level)1 Test (org.junit.Test)1