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])
| 178 | // the arguments, an ordered map of all of the flag names and their associated |
| 179 | // field objects, and any error. |
| 180 | func 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, |
no test coverage detected