A simple Java library for parsing command-line arguments.
sap4j is very simple to use. to start, make a Parser
instance, passing the pattern you want to look for in arguments.
// this parser will use the pattern '--', so it will find arguments starting with '--'.
private static final Parser parser = new Parser("--");
next, parse arguments from a String array.
// you can use the 'args' from a 'main' method, for example.
ArgumentList aList = parser.parseArguments(args);
now, you can check if an argument is present:
// don't include the pattern your parser uses! just include the name of the argument you're looking for.
boolean hasUsername = aList.argumentIsPresent("username");
if it's present, you can get it's value:
// use 'getArgument()' to get an Argument
Argument usernameArg = aList.getArgument("username");
// use this assertion if you've already checked that the argument is present.
assert usernameArg != null;
String username;
// you can use 'hasValue()' to check if an argument has a value. use 'getValue()' to get the String value of an Argument.
if (usernameArg.hasValue()) username = usernameArg.getValue();
else // ... do something else if there's no value
Add the appropriate dependency info to your project:
Jitpack repository
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
sap4j Dependency
<dependency>
<groupId>com.github.itstotallyjan</groupId>
<artifactId>sap4j</artifactId>
<version>{VERSION}</version>
</dependency>
Jitpack Repository
repositories {
maven { url 'https://jitpack.io' }
}
sap4j Dependency
dependencies {
implementation 'com.github.itstotallyjan:sap4j:{VERSION}'
}
Pull requests are accepted! As long as your contribution does not largely change the functionality of the library, I'll probably accept it.