MCPcopy
hub / github.com/spf13/pflag / Set

Method Set

flag.go:486–516  ·  view source on GitHub ↗

Set sets the value of the named flag.

(name, value string)

Source from the content-addressed store, hash-verified

484
485// Set sets the value of the named flag.
486func (f *FlagSet) Set(name, value string) error {
487 normalName := f.normalizeFlagName(name)
488 flag, ok := f.formal[normalName]
489 if !ok {
490 return &NotExistError{name: name, messageType: flagNoSuchFlagMessage}
491 }
492
493 err := flag.Value.Set(value)
494 if err != nil {
495 return &InvalidValueError{
496 flag: flag,
497 value: value,
498 cause: err,
499 }
500 }
501
502 if !flag.Changed {
503 if f.actual == nil {
504 f.actual = make(map[NormalizedName]*Flag)
505 }
506 f.actual[normalName] = flag
507 f.orderedActual = append(f.orderedActual, flag)
508
509 flag.Changed = true
510 }
511
512 if flag.Deprecated != "" {
513 fmt.Fprintf(f.Output(), "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
514 }
515 return nil
516}
517
518// SetAnnotation allows one to set arbitrary annotations on a flag in the FlagSet.
519// This is sometimes used by spf13/cobra programs which want to generate additional

Callers 3

TestVisitFlagOrderFunction · 0.95
ParseMethod · 0.95

Calls 3

normalizeFlagNameMethod · 0.95
OutputMethod · 0.95
SetMethod · 0.65

Tested by 2

TestVisitFlagOrderFunction · 0.76