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

Function SetFromArgs

cli/args.go:44–67  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

42// It is recommended that the [ErrNotFound] and [NoErrNotFound]
43// constants be used for the value of errNotFound for clearer code.
44func 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

Callers 8

LoadFromCommentFunction · 0.92
InspectForTypeMethod · 0.92
cmdFromFuncFunction · 0.85
configFunction · 0.85
TestSetFromArgsFunction · 0.85
TestSetFromArgsErrFunction · 0.85
TestUnusedArgFunction · 0.85
TestNoErrNotFoundFunction · 0.85

Calls 5

boolFlagsFunction · 0.85
getArgsFunction · 0.85
parseArgsFunction · 0.85
parseFlagsFunction · 0.85
CopyMethod · 0.65

Tested by 4

TestSetFromArgsFunction · 0.68
TestSetFromArgsErrFunction · 0.68
TestUnusedArgFunction · 0.68
TestNoErrNotFoundFunction · 0.68