Skip to content

Commit

Permalink
Add an example and use it in the README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 30, 2016
1 parent 65a489b commit 75b6b82
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
-->
![jcommander-addons](https://github.com/garydgregory/jcommander-addons/raw/master/src/site/resources/images/logo.png "jcommander-addons")

[JCommander](http://jcommander.org/) is a small Java framework that makes it simple to parse command line parameters.

The goal of the JCommander-Addons project is to provide any and all [JCommander](http://jcommander.org/)
converters and validators an advanced applications will need.

Expand All @@ -29,17 +31,50 @@ Feel free to submit a pull-request and examine the [sources](https://github.com/
[![Coverage Status](https://coveralls.io/repos/github/garydgregory/jcommander-addons/badge.svg?branch=master)](https://coveralls.io/github/garydgregory/jcommander-addons?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.garygregory/jcommander-addons/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.garygregory/jcommander-addons)

## Example
## Examples

To convert a command line argument into a Duration object, you write:
To convert a command line argument into a ```Duration``` object, you write:

```
@Parameter(names = { "--duration" }, converter = DurationConverter.class)
private Duration duration;
```

Here is a complete program using a ```LocalDate```:

```
package com.garygregory.jcommander.examples;
import java.time.LocalDate;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.garygregory.jcommander.converters.time.LocalDateConverter;
public class SimpleExample {
@Parameter(
names = { "--dob", "-d" },
converter = LocalDateConverter.class,
required = true,
description = "Birthday in the format YYYY-MM-DD, for example 2007-12-03")
LocalDate localDate;
public static void main(String... args) {
SimpleExample main = new SimpleExample();
new JCommander(main, args);
main.run();
}
public void run() {
System.out.printf("You were born on a %s.", localDate.getDayOfWeek());
}
}
```

## Converters
Converters include:
Our Converters include:

### Crypto Converters

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2016 Gary Gregory. All rights reserved.
*
* See the NOTICE.txt file distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.garygregory.jcommander.examples;

import java.time.LocalDate;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.garygregory.jcommander.converters.time.LocalDateConverter;

public class SimpleExample {

@Parameter(names = { "--dob",
"-d" }, converter = LocalDateConverter.class, required = true, description = "Birthday in the format YYYY-MM-DD, for example 2007-12-03")
LocalDate localDate;

public static void main(String... args) {
SimpleExample main = new SimpleExample();
new JCommander(main, args);
main.run();
}

public void run() {
System.out.printf("You were born on a %s.", localDate.getDayOfWeek());
}

}

0 comments on commit 75b6b82

Please sign in to comment.