Skip to content
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

Fix #1351 #1362

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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) {

remkop marked this conversation as resolved.
Show resolved Hide resolved
boolean flag = false;
wtd2 marked this conversation as resolved.
Show resolved Hide resolved
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);
wtd2 marked this conversation as resolved.
Show resolved Hide resolved
}
}