Last active
November 23, 2018 03:06
-
-
Save preytaren/0b438abd078058b81ab3508be1ff678e to your computer and use it in GitHub Desktop.
Read flag string like "a, b, c" to [a, b, c]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// used for parsing slice from flag input string | |
// read flag string like "a, b, c" and output [a, b, c] | |
package main | |
import ( | |
"fmt" | |
"strings" | |
"flag" | |
) | |
var f ArrayFlag | |
func init() { | |
flag.Var(&f, "arrayflags", "array flag test") | |
} | |
type ArrayFlag []string | |
func (a *ArrayFlag) String() string { | |
return fmt.Sprint(*a) | |
} | |
func (a *ArrayFlag) Set(val string) error { | |
tmp := strings.Split(val, ",") | |
for i:=0; i<len(tmp); i++ { | |
tmp[i] = strings.Trim(tmp[i], " ") | |
} | |
*a = ArrayFlag(tmp) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment