cli-parser


Command Line Interface Parser for Java

New Home

CLI Parser on Github

Description

Using annotations you can make very succinct main methods that don't need to know how to parse command line arguments with either fields, properties, or method based injection.

Very simple to use, very small dependency.

LOC

http://www.javarants.com/cli-parser-stats/loc.png

Example from the Tests

``` public static class TestCommand { @Argument(value = "input", description = "This is the input file", required = true) private String inputFilename;

    @Argument(value = "output", alias = "o", description = "This is the output file", required = true)
    private File outputFile;

    @Argument(description = "This flag can optionally be set")
    private boolean someflag;

    @Argument(description = "Minimum", alias = "m")
    private Integer minimum;

    @Argument(description = "List of values", delimiter = ":")
    public void setValues(Integer[] values) {
        this.values = values;
    }
    public Integer[] getValues() {
        return values;
    }
    private Integer[] values;

    @Argument(description = "List of strings", delimiter = ";")
    private String[] strings;

    @Argument(description = "not required")
    private boolean notRequired;

}

public void testArgsParse() {
    TestCommand tc = new TestCommand();
    Args.usage(tc);
    String[] args = {"-input", "inputfile", "-o", "outputfile", "extra1", "-someflag", "extra2", "-m", "10", "-values", "1:2:3", "-strings", "sam;dave;jolly"};
    List<String> extra = Args.parse(tc, args);
    assertEquals("inputfile", tc.inputFilename);
    assertEquals(new File("outputfile"), tc.outputFile);
    assertEquals(true, tc.someflag);
    assertEquals(10, tc.minimum.intValue());
    assertEquals(3, tc.values.length);
    assertEquals(2, tc.values[1].intValue());
    assertEquals("dave", tc.strings[1]);
    assertEquals(2, extra.size());
}

```

Project Information

Labels:
java cli parser