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 all commits
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
18 changes: 14 additions & 4 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8680,10 +8680,20 @@ public String[] description() {
private String[] expandVariables(String[] desc) {
if (desc.length == 0) { return desc; }
StringBuilder candidates = new StringBuilder();
if (completionCandidates() != null) {
for (String c : completionCandidates()) {
if (candidates.length() > 0) { candidates.append(", "); }
candidates.append(c);
boolean isCompletionCandidatesUsed = false;
remkop marked this conversation as resolved.
Show resolved Hide resolved
for (String s: desc) {
wtd2 marked this conversation as resolved.
Show resolved Hide resolved
if (s.contains(DESCRIPTION_VARIABLE_COMPLETION_CANDIDATES)) {
isCompletionCandidatesUsed = true;
break;
}
}
if (isCompletionCandidatesUsed) {
Iterable<String> iter = completionCandidates();
if (iter != null) {
for (String c : iter) {
if (candidates.length() > 0) { candidates.append(", "); }
candidates.append(c);
}
}
}
String defaultValueString = defaultValueString(false); // interpolate later
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/picocli/Issue1351.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 boolean testUsed;
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.
testUsed = true;
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 TestCommandWithoutCompletion {
@CommandLine.Option(names = "-o", completionCandidates = MyIterable.class,
description = "Candidates: A, B, C")
String option;
}

@CommandLine.Command
class TestCommandWithCompletion{
@CommandLine.Option(names = "-o", completionCandidates = MyIterable.class,
description = "Candidates: ${COMPLETION-CANDIDATES}")
String option;
}

@Test
public void testCompletionCandidatesUnused() {
testUsed = false;
CommandLine.usage(new TestCommandWithoutCompletion(), System.out);
assertEquals(false, testUsed);
}

@Test
public void testCompletionCandidatesUsed(){
testUsed = false;
CommandLine.usage(new TestCommandWithCompletion(), System.out);
assertEquals(true, testUsed);
}
}