From 0ecf4e7d0e0cfd43bba94b58c57f68117f5fa3d0 Mon Sep 17 00:00:00 2001 From: andreaTP Date: Tue, 23 Nov 2021 17:53:31 +0000 Subject: [PATCH] Remove execution and extension characters in the generated autocomplete script --- src/main/java/picocli/AutoComplete.java | 8 ++++++++ src/test/java/picocli/AutoCompleteTest.java | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/picocli/AutoComplete.java b/src/main/java/picocli/AutoComplete.java index dd267fb0e..5a5892933 100644 --- a/src/main/java/picocli/AutoComplete.java +++ b/src/main/java/picocli/AutoComplete.java @@ -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 @@ -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)); diff --git a/src/test/java/picocli/AutoCompleteTest.java b/src/test/java/picocli/AutoCompleteTest.java index 78eb118d5..bf2f2e18c 100644 --- a/src/test/java/picocli/AutoCompleteTest.java +++ b/src/test/java/picocli/AutoCompleteTest.java @@ -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")