MCPcopy Create free account
hub / github.com/cogentcore/core / parseArgs

Function parseArgs

cli/args.go:180–209  ·  view source on GitHub ↗

parseArgs parses the given non-flag arguments in the context of the given configuration struct, flags, and commands. The non-flag arguments and flags should be gotten through [getArgs] first. It returns the command specified by the arguments, an ordered map of all of the flag names and their associa

(cfg T, args []string, flags map[string]string, cmds ...*Cmd[T])

Source from the content-addressed store, hash-verified

178// the arguments, an ordered map of all of the flag names and their associated
179// field objects, and any error.
180func parseArgs[T any](cfg T, args []string, flags map[string]string, cmds ...*Cmd[T]) (cmd string, allFlags *fields, err error) {
181 newArgs, newCmd, err := parseArgsImpl(cfg, args, "", cmds...)
182 if err != nil {
183 return newCmd, allFlags, err
184 }
185
186 // if the command is blank, then it is the root command
187 if newCmd == "" {
188 for _, c := range cmds {
189 if c.Root && c.Name != "help" {
190 newCmd = c.Name
191 break
192 }
193 }
194 }
195
196 allFields := &fields{}
197 addMetaConfigFields(allFields)
198 addFields(cfg, allFields, newCmd)
199
200 allFlags = &fields{}
201 newArgs, err = addFlags(allFields, allFlags, newArgs, flags)
202 if err != nil {
203 return newCmd, allFields, err
204 }
205 if len(newArgs) > 0 {
206 return newCmd, allFields, fmt.Errorf("got unused arguments: %v", newArgs)
207 }
208 return newCmd, allFlags, nil
209}
210
211// parseArgsImpl is the underlying implementation of [parseArgs] that is called
212// recursively and takes most of what [parseArgs] does, plus the current command state,

Callers 1

SetFromArgsFunction · 0.85

Calls 5

parseArgsImplFunction · 0.85
addMetaConfigFieldsFunction · 0.85
addFieldsFunction · 0.85
addFlagsFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected