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)
| 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. |
| 1166 | func (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 | |
| 1202 | type parseFunc func(flag *Flag, value string) error |
| 1203 |