Skip to content

Commit

Permalink
picocli fish completion
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-v authored and volkov committed Jan 15, 2023
1 parent f0cb251 commit 9c4e8a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ private static class CommandDescriptor {
this.commandName = commandName;
this.commandLine = commandLine;
}

@Override
public String toString() {
return "CommandDescriptor{" +
"functionName='" + functionName + '\'' +
", parentFunctionName='" + parentFunctionName + '\'' +
", parentWithoutTopLevelCommand='" + parentWithoutTopLevelCommand + '\'' +
", commandName='" + commandName + '\'' +
", commandLine=" + commandLine +
'}';
}
}

private static final String SCRIPT_HEADER = "" +
Expand Down Expand Up @@ -534,6 +545,24 @@ public static String bash(String scriptName, CommandLine commandLine) {
return result.toString();
}


public static String fish(String scriptName, CommandLine commandLine) {
if (scriptName == null) { throw new NullPointerException("scriptName"); }
if (commandLine == null) { throw new NullPointerException("commandLine"); }
List<CommandDescriptor> hierarchy = createHierarchy(scriptName, commandLine);
//print hierarchy
for (CommandDescriptor descriptor : hierarchy) {
System.out.println(descriptor.functionName + " " + descriptor.commandName);
}

StringBuilder result = new StringBuilder();
result.append("Hello from fish!").append("\n");
for (CommandDescriptor commandDescriptor : hierarchy) {
result.append(commandDescriptor.toString()).append("\n");
}
return result.toString();
}

private static List<CommandDescriptor> createHierarchy(String scriptName, CommandLine commandLine) {
List<CommandDescriptor> result = new ArrayList<CommandDescriptor>();
result.add(new CommandDescriptor("_picocli_" + scriptName, "", "", scriptName, commandLine));
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2117,4 +2117,20 @@ public void testIssue1388_AliasesCommand() throws FileNotFoundException {
existingScript.delete();
}
}

@Test
public void testFish() {
String expected = String.format("hello from fish%n");

assertEquals(
expected,
AutoComplete.fish("myapp", new CommandLine(new Issue1352CommandWithResourceBundle()))
);

assertEquals(
expected,
AutoComplete.fish("myapp", new CommandLine(new Issue1352ParentCommand()))
);

}
}

0 comments on commit 9c4e8a2

Please sign in to comment.