use of joptsimple.OptionParser in project jopt-simple by jopt-simple.
the class OptionArgumentValueTypeTest method convertsArgumentsToJavaValueTypes.
@Test
public void convertsArgumentsToJavaValueTypes() {
OptionParser parser = new OptionParser();
parser.accepts("flag");
parser.accepts("count").withRequiredArg().ofType(Integer.class);
parser.accepts("level").withOptionalArg().ofType(Level.class);
OptionSet options = parser.parse("--count", "3", "--level", "DEBUG");
assertTrue(options.has("count"));
assertTrue(options.hasArgument("count"));
assertEquals(3, options.valueOf("count"));
assertTrue(options.has("level"));
assertTrue(options.hasArgument("level"));
assertEquals(Level.DEBUG, options.valueOf("level"));
}
use of joptsimple.OptionParser 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");
}
use of joptsimple.OptionParser in project jopt-simple by jopt-simple.
the class RequiredIfExample method main.
public static void main(String[] args) {
OptionParser parser = new OptionParser();
parser.accepts("ftp");
parser.accepts("username").requiredIf("ftp").withRequiredArg();
parser.accepts("password").requiredIf("ftp").withRequiredArg();
parser.parse("--ftp");
}
use of joptsimple.OptionParser in project jopt-simple by jopt-simple.
the class RequiredOptionsTest method allowsSpecificationOfRequiredOptions.
@Test(expected = OptionException.class)
public void allowsSpecificationOfRequiredOptions() {
OptionParser parser = new OptionParser() {
{
accepts("userid").withRequiredArg().required();
accepts("password").withRequiredArg().required();
}
};
parser.parse("--userid", "bob");
}
use of joptsimple.OptionParser in project jopt-simple by jopt-simple.
the class RequiredUnlessExample method main.
public static void main(String[] args) {
OptionParser parser = new OptionParser();
parser.accepts("anonymous");
parser.accepts("username").requiredUnless("anonymous").withRequiredArg();
parser.accepts("password").requiredUnless("anonymous").withRequiredArg();
parser.parse("--anonymous");
}
Aggregations