From 1b401ede188d2016d1f051dd3f97d68120c726f1 Mon Sep 17 00:00:00 2001
From: Jonathan Conder <jonathan.conder@canonical.com>
Date: Wed, 12 Mar 2025 13:30:46 +1300
Subject: [PATCH] fix: Prevent getCompletions from modifying os.Args

---
 completions.go | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/completions.go b/completions.go
index a1752f76..e0ef5bbd 100644
--- a/completions.go
+++ b/completions.go
@@ -359,7 +359,9 @@ func (c *Command) getCompletions(args []string) (*Command, []Completion, ShellCo
 	// if -- was already set or interspersed is false and there is already one arg then
 	// the extra added -- is counted as arg.
 	flagCompletion := true
-	_ = finalCmd.ParseFlags(append(finalArgs, "--"))
+	extendedArgs := make([]string, 0, len(finalArgs)+1)
+	extendedArgs = append(append(extendedArgs, finalArgs...), "--")
+	_ = finalCmd.ParseFlags(extendedArgs)
 	newArgCount := finalCmd.Flags().NArg()
 
 	// Parse the flags early so we can check if required flags are set