package pflagimport ()// -- ipNetSlice Valuetype ipNetSliceValue struct { value *[]net.IPNet changed bool}func newIPNetSliceValue( []net.IPNet, *[]net.IPNet) *ipNetSliceValue { := new(ipNetSliceValue) .value = *.value = return}// Set converts, and assigns, the comma-separated IPNet argument string representation as the []net.IPNet value of this flag.// If Set is called on a flag that already has a []net.IPNet assigned, the newly converted values will be appended.func ( *ipNetSliceValue) ( string) error {// remove all quote characters := strings.NewReplacer(`"`, "", `'`, "", "`", "")// read flag arguments with CSV parser , := readAsCSV(.Replace())if != nil && != io.EOF {return }// parse ip values into slice := make([]net.IPNet, 0, len())for , := range { , , := net.ParseCIDR(strings.TrimSpace())if != nil {returnfmt.Errorf("invalid string being converted to CIDR: %s", ) } = append(, *) }if !.changed { *.value = } else { *.value = append(*.value, ...) } .changed = truereturnnil}// Type returns a string that uniquely represents this flag's type.func ( *ipNetSliceValue) () string {return"ipNetSlice"}// String defines a "native" format for this net.IPNet slice flag value.func ( *ipNetSliceValue) () string { := make([]string, len(*.value))for , := range *.value { [] = .String() } , := writeAsCSV()return"[" + + "]"}func ipNetSliceConv( string) (interface{}, error) { = strings.Trim(, "[]")// Emtpy string would cause a slice with one (empty) entryiflen() == 0 {return []net.IPNet{}, nil } := strings.Split(, ",") := make([]net.IPNet, len())for , := range { , , := net.ParseCIDR(strings.TrimSpace())if != nil {returnnil, fmt.Errorf("invalid string being converted to CIDR: %s", ) } [] = * }return , nil}// GetIPNetSlice returns the []net.IPNet value of a flag with the given namefunc ( *FlagSet) ( string) ([]net.IPNet, error) { , := .getFlagType(, "ipNetSlice", ipNetSliceConv)if != nil {return []net.IPNet{}, }return .([]net.IPNet), nil}// IPNetSliceVar defines a ipNetSlice flag with specified name, default value, and usage string.// The argument p points to a []net.IPNet variable in which to store the value of the flag.func ( *FlagSet) ( *[]net.IPNet, string, []net.IPNet, string) { .VarP(newIPNetSliceValue(, ), , "", )}// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.func ( *FlagSet) ( *[]net.IPNet, , string, []net.IPNet, string) { .VarP(newIPNetSliceValue(, ), , , )}// IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string.// The argument p points to a []net.IPNet variable in which to store the value of the flag.func ( *[]net.IPNet, string, []net.IPNet, string) {CommandLine.VarP(newIPNetSliceValue(, ), , "", )}// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.func ( *[]net.IPNet, , string, []net.IPNet, string) {CommandLine.VarP(newIPNetSliceValue(, ), , , )}// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.// The return value is the address of a []net.IPNet variable that stores the value of that flag.func ( *FlagSet) ( string, []net.IPNet, string) *[]net.IPNet { := []net.IPNet{} .IPNetSliceVarP(&, , "", , )return &}// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.func ( *FlagSet) (, string, []net.IPNet, string) *[]net.IPNet { := []net.IPNet{} .IPNetSliceVarP(&, , , , )return &}// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.// The return value is the address of a []net.IP variable that stores the value of the flag.func ( string, []net.IPNet, string) *[]net.IPNet {returnCommandLine.IPNetSliceP(, "", , )}// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.func (, string, []net.IPNet, string) *[]net.IPNet {returnCommandLine.IPNetSliceP(, , , )}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.