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

Method Parse

flag.go:1166–1200  ·  view source on GitHub ↗

Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help was set but not defined.

(arguments []string)

Source from the content-addressed store, hash-verified

1164// are defined and before flags are accessed by the program.
1165// The return value will be ErrHelp if -help was set but not defined.
1166func (f *FlagSet) Parse(arguments []string) error {
1167 if f.addedGoFlagSets != nil {
1168 for _, goFlagSet := range f.addedGoFlagSets {
1169 goFlagSet.Parse(nil)
1170 }
1171 }
1172 f.parsed = true
1173
1174 f.args = make([]string, 0, len(arguments))
1175
1176 if len(arguments) == 0 {
1177 return nil
1178 }
1179
1180 set := func(flag *Flag, value string) error {
1181 return f.Set(flag.Name, value)
1182 }
1183
1184 err := f.parseArgs(arguments, set)
1185 if err != nil {
1186 switch f.errorHandling {
1187 case ContinueOnError:
1188 return err
1189 case ExitOnError:
1190 if err == ErrHelp {
1191 os.Exit(0)
1192 }
1193 fmt.Fprintln(f.Output(), err)
1194 os.Exit(2)
1195 case PanicOnError:
1196 panic(err)
1197 }
1198 }
1199 return nil
1200}
1201
1202type parseFunc func(flag *Flag, value string) error
1203

Callers 15

TestShorthandFunction · 0.95
TestShorthandLookupFunction · 0.95
TestParseRepeatedFunction · 0.95
TestChangedHelperFunction · 0.95
TestUserDefinedFunction · 0.95
TestSetOutputFunction · 0.95
TestHelpFunction · 0.95
TestNoInterspersedFunction · 0.95
TestTerminationFunction · 0.95
TestPrintDefaultsFunction · 0.95

Calls 4

SetMethod · 0.95
parseArgsMethod · 0.95
OutputMethod · 0.95
ParseMethod · 0.65

Tested by 15

TestShorthandFunction · 0.76
TestShorthandLookupFunction · 0.76
TestParseRepeatedFunction · 0.76
TestChangedHelperFunction · 0.76
TestUserDefinedFunction · 0.76
TestSetOutputFunction · 0.76
TestHelpFunction · 0.76
TestNoInterspersedFunction · 0.76
TestTerminationFunction · 0.76
TestPrintDefaultsFunction · 0.76