package pflag
import (
"fmt"
"io"
"net"
"strings"
)
type ipSliceValue struct {
value *[]net .IP
changed bool
}
func newIPSliceValue(val []net .IP , p *[]net .IP ) *ipSliceValue {
ipsv := new (ipSliceValue )
ipsv .value = p
*ipsv .value = val
return ipsv
}
func (s *ipSliceValue ) Set (val string ) error {
rmQuote := strings .NewReplacer (`"` , "" , `'` , "" , "`" , "" )
ipStrSlice , err := readAsCSV (rmQuote .Replace (val ))
if err != nil && err != io .EOF {
return err
}
out := make ([]net .IP , 0 , len (ipStrSlice ))
for _ , ipStr := range ipStrSlice {
ip := net .ParseIP (strings .TrimSpace (ipStr ))
if ip == nil {
return fmt .Errorf ("invalid string being converted to IP address: %s" , ipStr )
}
out = append (out , ip )
}
if !s .changed {
*s .value = out
} else {
*s .value = append (*s .value , out ...)
}
s .changed = true
return nil
}
func (s *ipSliceValue ) Type () string {
return "ipSlice"
}
func (s *ipSliceValue ) String () string {
ipStrSlice := make ([]string , len (*s .value ))
for i , ip := range *s .value {
ipStrSlice [i ] = ip .String ()
}
out , _ := writeAsCSV (ipStrSlice )
return "[" + out + "]"
}
func (s *ipSliceValue ) fromString (val string ) (net .IP , error ) {
return net .ParseIP (strings .TrimSpace (val )), nil
}
func (s *ipSliceValue ) toString (val net .IP ) string {
return val .String ()
}
func (s *ipSliceValue ) Append (val string ) error {
i , err := s .fromString (val )
if err != nil {
return err
}
*s .value = append (*s .value , i )
return nil
}
func (s *ipSliceValue ) Replace (val []string ) error {
out := make ([]net .IP , len (val ))
for i , d := range val {
var err error
out [i ], err = s .fromString (d )
if err != nil {
return err
}
}
*s .value = out
return nil
}
func (s *ipSliceValue ) GetSlice () []string {
out := make ([]string , len (*s .value ))
for i , d := range *s .value {
out [i ] = s .toString (d )
}
return out
}
func ipSliceConv(val string ) (interface {}, error ) {
val = strings .Trim (val , "[]" )
if len (val ) == 0 {
return []net .IP {}, nil
}
ss := strings .Split (val , "," )
out := make ([]net .IP , len (ss ))
for i , sval := range ss {
ip := net .ParseIP (strings .TrimSpace (sval ))
if ip == nil {
return nil , fmt .Errorf ("invalid string being converted to IP address: %s" , sval )
}
out [i ] = ip
}
return out , nil
}
func (f *FlagSet ) GetIPSlice (name string ) ([]net .IP , error ) {
val , err := f .getFlagType (name , "ipSlice" , ipSliceConv )
if err != nil {
return []net .IP {}, err
}
return val .([]net .IP ), nil
}
func (f *FlagSet ) IPSliceVar (p *[]net .IP , name string , value []net .IP , usage string ) {
f .VarP (newIPSliceValue (value , p ), name , "" , usage )
}
func (f *FlagSet ) IPSliceVarP (p *[]net .IP , name , shorthand string , value []net .IP , usage string ) {
f .VarP (newIPSliceValue (value , p ), name , shorthand , usage )
}
func IPSliceVar (p *[]net .IP , name string , value []net .IP , usage string ) {
CommandLine .VarP (newIPSliceValue (value , p ), name , "" , usage )
}
func IPSliceVarP (p *[]net .IP , name , shorthand string , value []net .IP , usage string ) {
CommandLine .VarP (newIPSliceValue (value , p ), name , shorthand , usage )
}
func (f *FlagSet ) IPSlice (name string , value []net .IP , usage string ) *[]net .IP {
p := []net .IP {}
f .IPSliceVarP (&p , name , "" , value , usage )
return &p
}
func (f *FlagSet ) IPSliceP (name , shorthand string , value []net .IP , usage string ) *[]net .IP {
p := []net .IP {}
f .IPSliceVarP (&p , name , shorthand , value , usage )
return &p
}
func IPSlice (name string , value []net .IP , usage string ) *[]net .IP {
return CommandLine .IPSliceP (name , "" , value , usage )
}
func IPSliceP (name , shorthand string , value []net .IP , usage string ) *[]net .IP {
return CommandLine .IPSliceP (name , shorthand , value , usage )
}
The pages are generated with Golds v0.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 .