Commands that start with a - should cause complete -p to add the --
delimiter for end of options before the command name.

    $ ./bash <<< $'complete -F _func -- -dashed\ncomplete -p -- -dashed'
    complete -F _func -- -dashed
---
 builtins/complete.def | 22 ++++++++++++++--------
 tests/complete.right  |  1 +
 tests/complete.tests  |  4 ++++
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/builtins/complete.def b/builtins/complete.def
index 236cd42c..ffe53b56 100644
--- a/builtins/complete.def
+++ b/builtins/complete.def
@@ -560,14 +560,20 @@ print_cmd_name (const char *cmd)
     printf ("-I");
   else if (*cmd == 0) /* XXX - can this happen? */
     printf ("''");
-  else if (sh_contains_shell_metas (cmd))
-    {
-      x = sh_single_quote (cmd);
-      printf ("%s", x);
-      free (x);
-    }
-  else
-    printf ("%s", cmd);
+  else {
+    char *fmt = "%s";
+    if (*cmd == '-')
+      fmt = "-- %s";
+
+    if (sh_contains_shell_metas (cmd))
+      {
+        x = sh_single_quote (cmd);
+        printf (fmt, x);
+        free (x);
+      }
+    else
+      printf (fmt, cmd);
+  }
 }

 static int
diff --git a/tests/complete.right b/tests/complete.right
index 1b7893b9..fc0290f6 100644
--- a/tests/complete.right
+++ b/tests/complete.right
@@ -385,3 +385,4 @@ compgen: usage: compgen [-V varname]
[-abcdefgjksuv] [-o option] [-A action] [-G
 compgen: usage: compgen [-V varname] [-abcdefgjksuv] [-o option] [-A
action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X
filterpat] [-P prefix] [-S suffix] [word]
 ./complete.tests: line 157: compgen: nooption: invalid option name
 ./complete.tests: line 159: compopt: nooption: invalid option name
+complete -F _func -- -dashcmd
diff --git a/tests/complete.tests b/tests/complete.tests
index adb7761a..3b968e58 100644
--- a/tests/complete.tests
+++ b/tests/complete.tests
@@ -157,3 +157,7 @@ compgen -D
 compgen -o nooption

 compopt -o nooption
+
+# test complete -p with command names starting with -
+complete -F _func -- -dashcmd
+complete -p -- -dashcmd
-- 
2.53.0

Reply via email to