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

Method AddFlag

flag.go:871–907  ·  view source on GitHub ↗

AddFlag will add the flag to the FlagSet

(flag *Flag)

Source from the content-addressed store, hash-verified

869
870// AddFlag will add the flag to the FlagSet
871func (f *FlagSet) AddFlag(flag *Flag) {
872 normalizedFlagName := f.normalizeFlagName(flag.Name)
873
874 _, alreadyThere := f.formal[normalizedFlagName]
875 if alreadyThere {
876 msg := fmt.Sprintf("%s flag redefined: %s", f.name, flag.Name)
877 fmt.Fprintln(f.Output(), msg)
878 panic(msg) // Happens only if flags are declared with identical names
879 }
880 if f.formal == nil {
881 f.formal = make(map[NormalizedName]*Flag)
882 }
883
884 flag.Name = string(normalizedFlagName)
885 f.formal[normalizedFlagName] = flag
886 f.orderedFormal = append(f.orderedFormal, flag)
887
888 if flag.Shorthand == "" {
889 return
890 }
891 if len(flag.Shorthand) > 1 {
892 msg := fmt.Sprintf("%q shorthand is more than one ASCII character", flag.Shorthand)
893 fmt.Fprintf(f.Output(), msg)
894 panic(msg)
895 }
896 if f.shorthands == nil {
897 f.shorthands = make(map[byte]*Flag)
898 }
899 c := flag.Shorthand[0]
900 used, alreadyThere := f.shorthands[c]
901 if alreadyThere {
902 msg := fmt.Sprintf("unable to redefine %q shorthand in %q flagset: it's already used for %q flag", c, f.name, used.Name)
903 fmt.Fprintf(f.Output(), msg)
904 panic(msg)
905 }
906 f.shorthands[c] = flag
907}
908
909// AddFlagSet adds one FlagSet to another. If a flag is already present in f
910// the flag from newSet will be ignored.

Callers 3

AddGoFlagMethod · 0.95
VarPFMethod · 0.95
AddFlagSetMethod · 0.95

Calls 2

normalizeFlagNameMethod · 0.95
OutputMethod · 0.95

Tested by

no test coverage detected