Debian Codegraph @

command_parse.go

raw symbols SCIP
1 package cliscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.22 fmt/"
5 "stringsscip-go gomod github.com/golang/go/src go1.22 strings/"
6 "unicodescip-go gomod github.com/golang/go/src go1.22 unicode/"
7 )
8
9 const (
10 providedButNotDefinedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/providedButNotDefinedErrMsg. = "flag provided but not defined: -"
11 argumentNotProvidedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/argumentNotProvidedErrMsg. = "flag needs an argument: "
12 )
13
14 // flagFromError tries to parse a provided flag from an error message. If the
15 // parsing fails, it returns the input error and an empty string
16 func flagFromErrorscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/flagFromError().(err error) (string, error) {
17 errStr := err.Error()
18 trimmed := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.22 strings/TrimPrefix().(errStr, providedButNotDefinedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/providedButNotDefinedErrMsg.)
19 if errStr == trimmed {
20 return "", err
21 }
22 return trimmed, nil
23 }
24
25 func (cmd *Commandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#) parseFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#parseFlags().(args Argsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Args#) (Argsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Args#, error) {
26 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parsing flags from arguments %[1]q (cmd=%[2]q)", args, cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
27
28 cmd.setFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#setFlags. = map[Flagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#]struct{}{}
29 cmd.appliedFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#appliedFlags. = cmd.allFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#allFlags().()
30
31 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("walking command lineage for persistent flags (cmd=%[1]q)", cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
32
33 for pCmd := cmd.parentscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#parent.; pCmd != nil; pCmd = pCmd.parentscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#parent. {
34 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().(
35 "checking ancestor command=%[1]q for persistent flags (cmd=%[2]q)",
36 pCmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name., cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.,
37 )
38
39 for _, fl := range pCmd.allFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#allFlags().() {
40 flNames := fl.Namesscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#Names.()
41
42 pfl, ok := fl.(LocalFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/LocalFlag#)
43 if !ok || pfl.IsLocalscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/LocalFlag#IsLocal.() {
44 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("skipping non-persistent flag %[1]q (cmd=%[2]q)", flNames, cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
45 continue
46 }
47
48 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().(
49 "checking for applying persistent flag=%[1]q pCmd=%[2]q (cmd=%[3]q)",
50 flNames, pCmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name., cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.,
51 )
52
53 applyPersistentFlag := true
54
55 for _, name := range flNames {
56 if cmd.lFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#lFlag().(name) != nil {
57 applyPersistentFlag = false
58 break
59 }
60 }
61
62 if !applyPersistentFlag {
63 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("not applying as persistent flag=%[1]q (cmd=%[2]q)", flNames, cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
64 continue
65 }
66
67 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("applying as persistent flag=%[1]q (cmd=%[2]q)", flNames, cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
68
69 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("appending to applied flags flag=%[1]q (cmd=%[2]q)", flNames, cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
70 cmd.appliedFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#appliedFlags. = append(cmd.appliedFlagsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#appliedFlags., fl)
71 }
72 }
73
74 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parsing flags iteratively tail=%[1]q (cmd=%[2]q)", args.Tailscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Args#Tail.(), cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
75 defer tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("done parsing flags (cmd=%[1]q)", cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name.)
76
77 posArgs := []string{}
78 for rargs := args.Slicescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Args#Slice.(); len(rargs) > 0; rargs = rargs[1:] {
79 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("rearrange:1 (cmd=%[1]q) %[2]q", cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name., rargs)
80
81 firstArg := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.22 strings/TrimSpace().(rargs[0])
82 if len(firstArg) == 0 {
83 posArgs = append(posArgs, rargs[0])
84 continue
85 }
86
87 // stop parsing once we see a "--"
88 if firstArg == "--" {
89 // In shell completion mode, preserve "--" so that completion can detect
90 // when the user is completing "--" itself vs. completing after "--"
91 if cmd.Rootscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Root().().shellCompletionscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#shellCompletion. {
92 posArgs = append(posArgs, firstArg)
93 }
94 posArgs = append(posArgs, rargs[1:]...)
95 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
96 }
97
98 // Check if we've reached the Nth argument and should stop flag parsing
99 if cmd.StopOnNthArgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#StopOnNthArg. != nil && len(posArgs) == *cmd.StopOnNthArgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#StopOnNthArg. {
100 // Append current arg and all remaining args without parsing
101 posArgs = append(posArgs, rargs[0:]...)
102 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
103 }
104
105 // handle positional args
106 if firstArg[0] != '-' {
107 // positional argument probably
108 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("rearrange-3 (cmd=%[1]q) check %[2]q", cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name., firstArg)
109
110 // if there is a command by that name let the command handle the
111 // rest of the parsing
112 if cmd.Commandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Command().(firstArg) != nil {
113 posArgs = append(posArgs, rargs...)
114 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
115 }
116
117 posArgs = append(posArgs, firstArg)
118 continue
119 }
120
121 numMinuses := 1
122 // this is same as firstArg == "-"
123 if len(firstArg) == 1 {
124 posArgs = append(posArgs, firstArg)
125 break
126 }
127
128 shortOptionHandling := cmd.useShortOptionHandlingscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#useShortOptionHandling().()
129
130 // stop parsing -- as short flags
131 if firstArg[1] == '-' {
132 numMinuses++
133 shortOptionHandling = false
134 } else if !unicodescip-go gomod github.com/golang/go/src go1.22 unicode/.IsLetterscip-go gomod github.com/golang/go/src go1.22 unicode/IsLetter().(rune(firstArg[1])) {
135 // this is not a flag
136 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parseFlags not a unicode letter. Stop parsing")
137 posArgs = append(posArgs, rargs...)
138 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
139 }
140
141 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parseFlags (shortOptionHandling=%[1]q)", shortOptionHandling)
142
143 flagName := firstArg[numMinuses:]
144 flagVal := ""
145 valFromEqual := false
146 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("flagName:1 (fName=%[1]q)", flagName)
147 if index := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.Indexscip-go gomod github.com/golang/go/src go1.22 strings/Index().(flagName, "="); index != -1 {
148 flagVal = flagName[index+1:]
149 flagName = flagName[:index]
150 valFromEqual = true
151 }
152
153 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("flagName:2 (fName=%[1]q) (fVal=%[2]q)", flagName, flagVal)
154
155 f := cmd.lookupAppliedFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#lookupAppliedFlag().(flagName)
156 // found a flag matching given flagName
157 if f != nil {
158 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("Trying flag type (fName=%[1]q) (type=%[2]T)", flagName, f)
159 if fb, ok := f.(boolFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/boolFlag#); ok && fb.IsBoolFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/boolFlag#IsBoolFlag.() {
160 if flagVal == "" {
161 flagVal = "true"
162 }
163 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parse Apply bool flag (fName=%[1]q) (fVal=%[2]q)", flagName, flagVal)
164 if err := cmd.setscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#set().(flagName, f, flagVal); err != nil {
165 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, err
166 }
167 continue
168 }
169
170 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("processing non bool flag (fName=%[1]q)", flagName)
171 // not a bool flag so need to get the next arg
172 if flagVal == "" && !valFromEqual {
173 if len(rargs) == 1 {
174 // In shell completion mode, preserve the flag so that DefaultCompleteWithFlags can use it
175 // as lastArg and offer suggestions for it.
176 if cmd.Rootscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Root().().shellCompletionscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#shellCompletion. {
177 posArgs = append(posArgs, rargs...)
178 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
179 }
180 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22 fmt/Errorf().("%s%s", argumentNotProvidedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/argumentNotProvidedErrMsg., firstArg)
181 }
182 flagVal = rargs[1]
183 rargs = rargs[1:]
184 }
185
186 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("setting non bool flag (fName=%[1]q) (fVal=%[2]q)", flagName, flagVal)
187 if err := cmd.setscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#set().(flagName, f, flagVal); err != nil {
188 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, err
189 }
190
191 continue
192 }
193
194 // no flag lookup found and short handling is disabled
195 if !shortOptionHandling {
196 // In shell completion mode, preserve the partial flag so that DefaultCompleteWithFlags can use it
197 // as lastArg and offer suggestions that match the prefix.
198 if cmd.Rootscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Root().().shellCompletionscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#shellCompletion. {
199 posArgs = append(posArgs, rargs...)
200 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
201 }
202 // When DefaultCommand is set, pass unknown flags through as positional args
203 // so the default command can handle them (fixes #2249)
204 if cmd.DefaultCommandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#DefaultCommand. != "" {
205 posArgs = append(posArgs, rargs...)
206 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
207 }
208 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22 fmt/Errorf().("%s%s", providedButNotDefinedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/providedButNotDefinedErrMsg., flagName)
209 }
210
211 // try to split the flags
212 for index, c := range flagName {
213 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("processing flag (fName=%[1]q)", string(c))
214 if sf := cmd.lookupFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#lookupFlag().(string(c)); sf == nil {
215 if index == 0 && cmd.DefaultCommandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#DefaultCommand. != "" {
216 posArgs = append(posArgs, rargs...)
217 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
218 }
219 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22 fmt/Errorf().("%s%s", providedButNotDefinedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/providedButNotDefinedErrMsg., flagName)
220 } else if fb, ok := sf.(boolFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/boolFlag#); ok && fb.IsBoolFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/boolFlag#IsBoolFlag.() {
221 fv := flagVal
222 if index == (len(flagName)-1) && flagVal == "" {
223 fv = "true"
224 }
225 if fv == "" {
226 fv = "true"
227 }
228 if err := cmd.setscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#set().(flagName, sf, fv); err != nil {
229 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("processing flag.2 (fName=%[1]q)", string(c))
230 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, err
231 }
232 } else if index == len(flagName)-1 { // last flag can take an arg
233 if flagVal == "" {
234 if len(rargs) == 1 {
235 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22 fmt/Errorf().("%s%s", argumentNotProvidedErrMsgscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/argumentNotProvidedErrMsg., string(c))
236 }
237 flagVal = rargs[1]
238 rargs = rargs[1:]
239 }
240 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("parseFlags (flagName %[1]q) (flagVal %[2]q)", flagName, flagVal)
241 if err := cmd.setscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#set().(flagName, sf, flagVal); err != nil {
242 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("processing flag.4 (fName=%[1]q)", string(c))
243 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, err
244 }
245 }
246 }
247 }
248
249 tracefscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/tracef().("returning-2 (cmd=%[1]q) args %[2]q", cmd.Namescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Name., posArgs)
250 return &stringSliceArgsscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/stringSliceArgs#{posArgs}, nil
251 }