SetFromArgs sets config values on the given config object from the given from command-line args, based on the field names in the config struct and the given list of available commands. It returns the command, if any, that was passed in the arguments, and any error than occurs during the parsing and
(cfg T, args []string, errNotFound bool, cmds ...*Cmd[T])
| 42 | // It is recommended that the [ErrNotFound] and [NoErrNotFound] |
| 43 | // constants be used for the value of errNotFound for clearer code. |
| 44 | func SetFromArgs[T any](cfg T, args []string, errNotFound bool, cmds ...*Cmd[T]) (string, error) { |
| 45 | bf := boolFlags(cfg) |
| 46 | // if we are not already a meta config object, we have to add |
| 47 | // all of the bool flags of the meta config object so that we |
| 48 | // correctly handle the short (no value) versions of things like |
| 49 | // verbose and quiet. |
| 50 | if _, ok := any(cfg).(*metaConfig); !ok { |
| 51 | mcf := boolFlags(&metaConfigFields{}) |
| 52 | maps.Copy(bf, mcf) |
| 53 | } |
| 54 | nfargs, flags, err := getArgs(args, bf) |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | cmd, allFlags, err := parseArgs(cfg, nfargs, flags, cmds...) |
| 59 | if err != nil { |
| 60 | return "", err |
| 61 | } |
| 62 | err = parseFlags(flags, allFlags, errNotFound) |
| 63 | if err != nil { |
| 64 | return "", err |
| 65 | } |
| 66 | return cmd, nil |
| 67 | } |
| 68 | |
| 69 | // boolFlags returns a map with a true value for every flag name |
| 70 | // that maps to a boolean field. This is needed so that bool |