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

Remove execution and extension characters in the generated autocomplete script #1477

Merged
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
8 changes: 8 additions & 0 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ private static class CommandDescriptor {
"# default Bash completions and the Readline default filename completions are performed.\n" +
"complete -F _complete_%1$s -o default %1$s %1$s.sh %1$s.bash\n";

private static String sanitizeScriptName(String scriptName) {
return scriptName
.replaceAll("\\.sh", "")
.replaceAll("\\.bash", "")
.replaceAll("\\.\\/", "");
}

/**
* Generates source code for an autocompletion bash script for the specified picocli-based application,
* and writes this script to the specified {@code out} file, and optionally writes an invocation script
Expand Down Expand Up @@ -464,6 +471,7 @@ public static void bash(String scriptName, File out, File command, CommandLine c
public static String bash(String scriptName, CommandLine commandLine) {
if (scriptName == null) { throw new NullPointerException("scriptName"); }
if (commandLine == null) { throw new NullPointerException("commandLine"); }
scriptName = sanitizeScriptName(scriptName);
StringBuilder result = new StringBuilder();
result.append(format(SCRIPT_HEADER, scriptName, CommandLine.VERSION));

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,18 @@ public void testBashify() {
assertEquals(expected, actual);
}

@Test
public void testBashifyWithExtras() {
CommandSpec cmd = CommandSpec.create().addOption(
OptionSpec.builder("-x")
.type(String.class)
.paramLabel("_A\tB C")
.completionCandidates(Arrays.asList("1")).build());
String actual = AutoComplete.bash("./bashify.sh", new CommandLine(cmd));
String expected = format(loadTextFromClasspath("/bashify_completion.bash"), CommandLine.VERSION);
assertEquals(expected, actual);
}

@Test
public void testBooleanArgFilter() {
@Command(name = "booltest")
Expand Down