Skip to content

Commit

Permalink
Revert "Fix remkop#1351 with some modification and another test"
Browse files Browse the repository at this point in the history
This reverts commit 1280ae7.
  • Loading branch information
MarkoMackic committed Oct 17, 2021
1 parent 72f569f commit b151041
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
24 changes: 12 additions & 12 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8680,19 +8680,19 @@ public String[] description() {
private String[] expandVariables(String[] desc) {
if (desc.length == 0) { return desc; }
StringBuilder candidates = new StringBuilder();
if (completionCandidates() != null) {
boolean isCompletionCandidatesUsed = false;
for (String s: desc) {
if (s.contains(DESCRIPTION_VARIABLE_COMPLETION_CANDIDATES)) {
isCompletionCandidatesUsed = true;
break;
}

boolean flag = false;
for (String s: desc) {
if (s.contains(DESCRIPTION_VARIABLE_COMPLETION_CANDIDATES)) {
flag = true;
break;
}
if (isCompletionCandidatesUsed){
for (String c : completionCandidates()) {
if (candidates.length() > 0) { candidates.append(", "); }
candidates.append(c);
}
}

if (completionCandidates() != null && flag) {
for (String c : completionCandidates()) {
if (candidates.length() > 0) { candidates.append(", "); }
candidates.append(c);
}
}
String defaultValueString = defaultValueString(false); // interpolate later
Expand Down
28 changes: 7 additions & 21 deletions src/test/java/picocli/Issue1351.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import static org.junit.Assert.assertEquals;

public class Issue1351 {
static boolean testUsed;
static int flag = 0;

static class MyIterator implements Iterator<String> {
private int cursor;
private final String[] a;
Expand All @@ -21,7 +22,7 @@ static class MyIterator implements Iterator<String> {
@Override
public boolean hasNext() {
// Do something in the iterator, maybe talking to a server as was mentioned in issue 1351.
testUsed = true;
flag = flag + 1;
return this.cursor < this.a.length;
}

Expand All @@ -45,30 +46,15 @@ public Iterator<String> iterator() {
}

@CommandLine.Command
class TestCommandWithoutCompletion {
class TestCommand {
@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);
public void testIssue1351() {
CommandLine.usage(new TestCommand(), System.out);
assertEquals(0, flag);
}
}

0 comments on commit b151041

Please sign in to comment.