Search in sources :

Example 41 with OptionParser

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"));
}
Also used : OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser) Test(org.junit.Test)

Example 42 with OptionParser

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");
}
Also used : Level(joptsimple.examples.Level) OptionSet(joptsimple.OptionSet) File(java.io.File) OptionParser(joptsimple.OptionParser) Test(org.junit.Test)

Example 43 with OptionParser

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");
}
Also used : OptionParser(joptsimple.OptionParser)

Example 44 with OptionParser

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");
}
Also used : OptionParser(joptsimple.OptionParser) Test(org.junit.Test)

Example 45 with OptionParser

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");
}
Also used : OptionParser(joptsimple.OptionParser)

Aggregations

OptionParser (joptsimple.OptionParser)199 OptionSet (joptsimple.OptionSet)151 File (java.io.File)58 IOException (java.io.IOException)29 Test (org.junit.Test)28 OptionException (joptsimple.OptionException)25 ArrayList (java.util.ArrayList)21 Properties (java.util.Properties)16 List (java.util.List)15 OptionSpec (joptsimple.OptionSpec)14 Test (org.junit.jupiter.api.Test)12 VerifiableProperties (com.github.ambry.config.VerifiableProperties)11 FileNotFoundException (java.io.FileNotFoundException)9 BufferedReader (java.io.BufferedReader)8 ArgumentAcceptingOptionSpec (joptsimple.ArgumentAcceptingOptionSpec)8 FileReader (java.io.FileReader)7 OptionSpecBuilder (joptsimple.OptionSpecBuilder)7 Closer (com.google.common.io.Closer)6 Cluster (voldemort.cluster.Cluster)6 StoreDefinition (voldemort.store.StoreDefinition)6