Skip to content
/ sap4j Public

A simple Java library for parsing command-line arguments.

Notifications You must be signed in to change notification settings

blueysh/sap4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sap4j

A simple Java library for parsing command-line arguments.

build last commit

how to use it

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

adding sap4j to your project

Add the appropriate dependency info to your project:

maven

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>

gradle

Jitpack Repository

repositories {
  maven { url 'https://jitpack.io' }
}

sap4j Dependency

dependencies {
  implementation 'com.github.itstotallyjan:sap4j:{VERSION}'
}

contributing

Pull requests are accepted! As long as your contribution does not largely change the functionality of the library, I'll probably accept it.