Skip to content

Commit

Permalink
Clarification: List and Set can be comma-separated
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Oct 2, 2023
1 parent 98512af commit e7bd5fc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
9 changes: 8 additions & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ When a Parameter annotation is found on a field of type `List` or `Set`, JComman

[source,java]
----
@Parameter(names = "-host", description = "The host")
@Parameter(names = {"-host", "-hosts"}, description = "Host option can be used multiple times, and may be comma-separated")
private List<String> hosts = new ArrayList<>();
----

Expand All @@ -169,6 +169,13 @@ will allow you to parse the following command line:
$ java Main -host host1 -verbose -host host2
----

Alternatively the parameters can be provided as a comma-separated list:

[source,bash]
----
$ java Main -hosts host1,host2
----

When JCommander is done parsing the line above, the field hosts will contain the strings `"host1"` and `"host2"`.

=== Password
Expand Down
12 changes: 10 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ <h3 id="_lists_and_sets">2.2. Lists and Sets</h3>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">@Parameter(names = "-host", description = "The host")
<pre class="prettyprint highlight"><code data-lang="java">@Parameter(names = {"-host", "-hosts"}, description = "Host option can be used multiple times, and may be comma-separated")
private List&lt;String&gt; hosts = new ArrayList&lt;&gt;();</code></pre>
</div>
</div>
Expand All @@ -966,6 +966,14 @@ <h3 id="_lists_and_sets">2.2. Lists and Sets</h3>
</div>
</div>
<div class="paragraph">
<p>Alternatively the parameters can be provided as a comma-separated list:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">$ java Main -hosts host1,host2</code></pre>
</div>
</div>
<div class="paragraph">
<p>When JCommander is done parsing the line above, the field hosts will contain the strings <code>"host1"</code> and <code>"host2"</code>.</p>
</div>
</div>
Expand Down Expand Up @@ -2187,7 +2195,7 @@ <h2 id="_download">29. Download</h2>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2023-10-02 09:35:01 UTC
Last updated 2023-10-02 12:48:25 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/beust/jcommander/ListOfPathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.beust.jcommander;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListOfPathTest {

@Test
public void testParse() {
// given
final class Args {
@Parameter(names = { "--paths"}, description = "List of paths separated by comma")
private List<Path> paths = Collections.emptyList();
}
final Args args = new Args();

// when
JCommander.newBuilder()
.addObject(args)
.build()
.parse("--paths", "/home/foo,/var/lib/bar");

// then
Assert.assertEquals(args.paths, Arrays.asList(Paths.get("/home/foo"), Paths.get("/var/lib/bar")));
}

public static void main(String[] args) {
new ListOfPathTest().testParse();
}
}
5 changes: 3 additions & 2 deletions src/test/java/com/beust/jcommander/SetOfEnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -27,8 +28,8 @@ class Args {
JCommander.newBuilder()
.addObject(args)
.build()
.parse("--season", "SPRING");
Assert.assertEquals(Season.class, args.seasons.toArray()[0].getClass());
.parse("--season", "SPRING,AUTUMN");
Assert.assertEquals(EnumSet.copyOf(Arrays.asList(Season.SPRING, Season.AUTUMN)), args.seasons);
}

public static void main(String[] args) {
Expand Down

0 comments on commit e7bd5fc

Please sign in to comment.