Skip to content

Updated README.md with usage example #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,102 @@ Then add JArgs to your `dependencies` list tag:
</dependency>
```

Usage Example
-------------

#### Creating and parsing

```
public static void main( String[] args ) {

// First, create a CmdLineParser
CmdLineParser parser = new CmdLineParser();

// Add Options
// Boolean Options like debug, and verbose do not have any associated values
Option<Boolean> debug = parser.addBooleanOption('d', "debug"); // Option with a short name and long name

Option<Boolean> verbose = parser.addBooleanOption("verbose"); // Option with just a long name

// The Options below have a value associated with it
Option<Integer> size = parser.addIntegerOption('s', "size");
Option<Double> fraction = parser.addDoubleOption('f', "fraction");


// To parse the Options
try {
parser.parse(args);
} catch (CmdLineParser.OptionException e) {
System.err.println(e.getMessage());
System.exit(-1);
}

// Getting values
Boolean debugValue = parser.getOptionValue(debug);

// Setting a default value if no input is provided
Integer sizeValue = parser.getOptionValue(size, new Integer(10));
}
```

#### Collection input
Parameters can be specified more than once and can be saved into a collection

For example, you can pass multiple listItems and save into a single collection

###### Program Arguments
```
--verbose --listItem Foo --listItem Bar
```

```
{
Option<String> listItem = parser.addIntegerOption("listItem");
Collection<String> fractionValues = parser.getOptionValues(listItem);
...
}
```


#### Values starting with '-'
Although Options keys start with '-', it's possible to pass negative numbers, also works for any string which starts with '-'

For example, you can pass negative numbers and negative fractions

###### Program Arguments
```
--verbose -n -100 --double -20.3
```

```
{
Option<Integer> size = parser.addIntegerOption('n', "number");
Option<Double> fraction = parser.addDoubleOption('d', "double");

Integer sizeValue = parser.getOptionValue(size);
Double d = parser.getOptionValue(fraction);
...
}
```

#### Collecting other remaining command line arguments


##### Remaining command line arguments can be captured by separating with '--'
```
--verbose --debug -n 100 -d 8.3 -d 8.4 -- -other -value

```

```
{
String[] otherArgs = parser.getRemainingArgs();
...
}
```



Documentation
-------------

Expand Down