|
| 1 | +# `sap4j` |
| 2 | +A simple Java library for parsing command-line arguments. |
| 3 | + |
| 4 | +### [`how to use`](#how-to-use-it) **|** [`adding to your project`](#adding-sap4j-to-your-project) **|** [`contributing`](#contributing) |
| 5 | + |
| 6 | +### `how to use it` |
| 7 | +sap4j is very simple to use. to start, make a `Parser` instance, passing the pattern you want to look for in arguments. |
| 8 | +```java |
| 9 | +// this parser will use the pattern '--', so it will find arguments starting with '--'. |
| 10 | +private static final Parser parser = new Parser("--"); |
| 11 | +``` |
| 12 | +next, parse arguments from a String array. |
| 13 | +```java |
| 14 | +// you can use the 'args' from a 'main' method, for example. |
| 15 | +ArgumentList aList = parser.parseArguments(args); |
| 16 | +``` |
| 17 | +now, you can check if an argument is present: |
| 18 | +```java |
| 19 | +// don't include the pattern your parser uses! just include the name of the argument you're looking for. |
| 20 | +boolean hasUsername = aList.argumentIsPresent("username"); |
| 21 | +``` |
| 22 | +if it's present, you can get it's value: |
| 23 | +```java |
| 24 | +// use 'getArgument()' to get an Argument |
| 25 | +Argument usernameArg = aList.getArgument("username"); |
| 26 | +// use this assertion if you've already checked that the argument is present. |
| 27 | +assert usernameArg != null; |
| 28 | + |
| 29 | +String username; |
| 30 | +// you can use 'hasValue()' to check if an argument has a value. use 'getValue()' to get the String value of an Argument. |
| 31 | +if (usernameArg.hasValue()) username = usernameArg.getValue(); |
| 32 | +else // ... do something else if there's no value |
| 33 | +``` |
| 34 | + |
| 35 | +### `adding sap4j to your project` |
| 36 | +Add the appropriate dependency info to your project: |
| 37 | +#### `maven` |
| 38 | +Jitpack repository |
| 39 | +```xml |
| 40 | +<repository> |
| 41 | + <id>jitpack.io</id> |
| 42 | + <url>https://jitpack.io</url> |
| 43 | +</repository> |
| 44 | +``` |
| 45 | +sap4j Dependency |
| 46 | +```xml |
| 47 | + <dependency> |
| 48 | + <groupId>com.github.itstotallyjan</groupId> |
| 49 | + <artifactId>sap4j</artifactId> |
| 50 | + <version>{VERSION}</version> |
| 51 | + </dependency> |
| 52 | +``` |
| 53 | +#### `gradle` |
| 54 | +Jitpack Repository |
| 55 | +```gradle |
| 56 | +repositories { |
| 57 | + maven { url 'https://jitpack.io' } |
| 58 | +} |
| 59 | +``` |
| 60 | +sap4j Dependency |
| 61 | +```gradle |
| 62 | +dependencies { |
| 63 | + implementation 'com.github.itstotallyjan:sap4j:-SNAPSHOT' |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### `contributing` |
| 68 | +Pull requests are accepted! As long as your contribution does not largely change the functionality of the library, I'll probably accept it. |
0 commit comments