@@ -10,6 +10,7 @@ import (
10
10
"strings"
11
11
12
12
"github.com/kballard/go-shellquote"
13
+ "github.com/spf13/cobra"
13
14
14
15
"github.com/lxc/incus/v6/internal/i18n"
15
16
config "github.com/lxc/incus/v6/shared/cliconfig"
@@ -76,26 +77,24 @@ func findAlias(aliases map[string]string, origArgs []string) ([]string, []string
76
77
return aliasKey , aliasValue , foundAlias
77
78
}
78
79
79
- func expandAlias (conf * config.Config , args []string ) ([]string , bool , error ) {
80
- var completion = false
81
- var completionFrament string
82
- var newArgs []string
83
- var origArgs []string
84
-
85
- for _ , arg := range args [1 :] {
86
- if ! strings .HasPrefix (arg , "-" ) {
87
- break
88
- }
89
-
90
- newArgs = append (newArgs , arg )
80
+ func expandAlias (conf * config.Config , app * cobra.Command ) ([]string , bool , error ) {
81
+ fset := app .Flags ()
82
+ if fset .NArg () == 0 {
83
+ return nil , false , nil
91
84
}
92
85
93
- origArgs = append ([]string {args [0 ]}, args [len (newArgs )+ 1 :]... )
86
+ // newArgs contains all the flags before the first positional argument
87
+ newArgs := os .Args [1 :slices .Index (os .Args , fset .Arg (0 ))]
88
+
89
+ // origArgs contains everything except the flags in newArgs
90
+ origArgs := append ([]string {os .Args [0 ]}, os .Args [len (newArgs )+ 1 :]... )
94
91
95
92
// strip out completion subcommand and fragment from end
93
+ completion := false
94
+ completionFragment := ""
96
95
if len (origArgs ) >= 3 && origArgs [1 ] == "__complete" {
97
96
completion = true
98
- completionFrament = origArgs [len (origArgs )- 1 ]
97
+ completionFragment = origArgs [len (origArgs )- 1 ]
99
98
origArgs = append (origArgs [:1 ], origArgs [2 :len (origArgs )- 1 ]... )
100
99
}
101
100
@@ -189,7 +188,7 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
189
188
// add back in completion if it was stripped before
190
189
if completion {
191
190
newArgs = append ([]string {newArgs [0 ], "__complete" }, newArgs [1 :]... )
192
- newArgs = append (newArgs , completionFrament )
191
+ newArgs = append (newArgs , completionFragment )
193
192
}
194
193
195
194
// Add the rest of the arguments only if @ARGS@ wasn't used.
@@ -200,9 +199,7 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
200
199
return newArgs , true , nil
201
200
}
202
201
203
- func execIfAliases () error {
204
- args := os .Args
205
-
202
+ func execIfAliases (app * cobra.Command ) error {
206
203
// Avoid loops
207
204
if os .Getenv ("INCUS_ALIASES" ) == "1" {
208
205
return nil
@@ -214,7 +211,7 @@ func execIfAliases() error {
214
211
}
215
212
216
213
// Expand the aliases
217
- newArgs , expanded , err := expandAlias (conf , args )
214
+ newArgs , expanded , err := expandAlias (conf , app )
218
215
if err != nil {
219
216
return err
220
217
} else if ! expanded {
0 commit comments