Cedric Beust | 2cc20eb | 2010-07-12 22:45:52 -0700 | [diff] [blame] | 1 | JCommander |
| 2 | ========== |
| 3 | |
Simon Legner | 2844c79 | 2016-10-05 14:54:13 +0200 | [diff] [blame] | 4 | This is an annotation based parameter parsing framework for Java 8. |
Cedric Beust | 2cc20eb | 2010-07-12 22:45:52 -0700 | [diff] [blame] | 5 | |
Cedric Beust | 01b687e | 2010-07-12 22:52:28 -0700 | [diff] [blame] | 6 | Here is a quick example: |
| 7 | |
Joe Littlejohn | 8767867 | 2012-07-14 01:39:19 +0200 | [diff] [blame] | 8 | ```java |
| 9 | public class JCommanderTest { |
| 10 | @Parameter |
| 11 | public List<String> parameters = Lists.newArrayList(); |
| 12 | |
| 13 | @Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity") |
| 14 | public Integer verbose = 1; |
| 15 | |
| 16 | @Parameter(names = "-groups", description = "Comma-separated list of group names to be run") |
| 17 | public String groups; |
| 18 | |
| 19 | @Parameter(names = "-debug", description = "Debug mode") |
| 20 | public boolean debug = false; |
John Yani | c55dc01 | 2012-10-25 12:33:30 +0300 | [diff] [blame] | 21 | |
| 22 | @DynamicParameter(names = "-D", description = "Dynamic parameters go here") |
| 23 | public Map<String, String> dynamicParams = new HashMap<String, String>(); |
| 24 | |
Joe Littlejohn | 8767867 | 2012-07-14 01:39:19 +0200 | [diff] [blame] | 25 | } |
| 26 | ``` |
Cedric Beust | 01b687e | 2010-07-12 22:52:28 -0700 | [diff] [blame] | 27 | |
| 28 | and how you use it: |
| 29 | |
Joe Littlejohn | 8767867 | 2012-07-14 01:39:19 +0200 | [diff] [blame] | 30 | ```java |
John Yani | f50612c | 2012-10-25 12:52:08 +0300 | [diff] [blame] | 31 | JCommanderTest jct = new JCommanderTest(); |
John Yani | c55dc01 | 2012-10-25 12:33:30 +0300 | [diff] [blame] | 32 | String[] argv = { "-log", "2", "-groups", "unit1,unit2,unit3", |
John Yani | f50612c | 2012-10-25 12:52:08 +0300 | [diff] [blame] | 33 | "-debug", "-Doption=value", "a", "b", "c" }; |
Joe Littlejohn | 8767867 | 2012-07-14 01:39:19 +0200 | [diff] [blame] | 34 | new JCommander(jct, argv); |
Cedric Beust | 01b687e | 2010-07-12 22:52:28 -0700 | [diff] [blame] | 35 | |
John Yani | c55dc01 | 2012-10-25 12:33:30 +0300 | [diff] [blame] | 36 | Assert.assertEquals(2, jct.verbose.intValue()); |
John Yani | c55dc01 | 2012-10-25 12:33:30 +0300 | [diff] [blame] | 37 | Assert.assertEquals("unit1,unit2,unit3", jct.groups); |
John Yani | 459da85 | 2012-10-25 12:43:46 +0300 | [diff] [blame] | 38 | Assert.assertEquals(true, jct.debug); |
John Yani | f50612c | 2012-10-25 12:52:08 +0300 | [diff] [blame] | 39 | Assert.assertEquals("value", jct.dynamicParams.get("option")); |
John Yani | c55dc01 | 2012-10-25 12:33:30 +0300 | [diff] [blame] | 40 | Assert.assertEquals(Arrays.asList("a", "b", "c"), jct.parameters); |
Joe Littlejohn | 8767867 | 2012-07-14 01:39:19 +0200 | [diff] [blame] | 41 | ``` |
Cedric Beust | 01b687e | 2010-07-12 22:52:28 -0700 | [diff] [blame] | 42 | |
Cedric Beust | 6b0be06 | 2015-11-05 21:09:06 -0800 | [diff] [blame] | 43 | The full doc is available at [http://jcommander.org](http://jcommander.org). |
Cedric Beust | 6db5bf4 | 2016-10-04 10:16:18 -0700 | [diff] [blame] | 44 | |
| 45 | ## Building JCommander |
| 46 | |
| 47 | ``` |
| 48 | ./kobaltw assemble |
| 49 | ``` |
| 50 | |