Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wtd2 authored and MarkoMackic committed Oct 17, 2021
1 parent 75e2aa3 commit 61eeba8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8680,7 +8680,16 @@ public String[] description() {
private String[] expandVariables(String[] desc) {
if (desc.length == 0) { return desc; }
StringBuilder candidates = new StringBuilder();
if (completionCandidates() != null) {

boolean flag = false;
for (String s: desc) {
if (s.contains(DESCRIPTION_VARIABLE_COMPLETION_CANDIDATES)) {
flag = true;
break;
}
}

if (completionCandidates() != null && flag) {
for (String c : completionCandidates()) {
if (candidates.length() > 0) { candidates.append(", "); }
candidates.append(c);
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/picocli/Issue1351.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package picocli;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

import static org.junit.Assert.assertEquals;

public class Issue1351 {
static int flag = 0;

static class MyIterator implements Iterator<String> {
private int cursor;
private final String[] a;

MyIterator(String[] a) {
this.a = a;
}

@Override
public boolean hasNext() {
// Do something in the iterator, maybe talking to a server as was mentioned in issue 1351.
flag = flag + 1;
return this.cursor < this.a.length;
}

@Override
public String next() {
int i = this.cursor;
if (i >= this.a.length) {
throw new NoSuchElementException();
} else {
this.cursor = i + 1;
return this.a[i];
}
}
}

static class MyIterable implements Iterable<String> {
@Override
public Iterator<String> iterator() {
return new MyIterator(new String[]{"A", "B", "C"});
}
}

@CommandLine.Command
class TestCommand {
@CommandLine.Option(names = "-o", completionCandidates = MyIterable.class,
description = "Candidates: A, B, C")
String option;
}

@Test
public void testIssue1351() {
CommandLine.usage(new TestCommand(), System.out);
assertEquals(0, flag);
}
}

0 comments on commit 61eeba8

Please sign in to comment.