Last active
December 8, 2020 19:09
-
-
Save andrewmeissner/e52ec19a8b8d253ee42a508c63cdbf2a to your computer and use it in GitHub Desktop.
CLI array flags using stdlib
This file contains hidden or 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
| type arrayFlags []string | |
| func (i *arrayFlags) String() string { | |
| return strings.Join(*i, ",") | |
| } | |
| func (i *arrayFlags) Set(value string) error { | |
| *i = append(*i, strings.Split(value, ",")...) | |
| return nil | |
| } | |
| var sliceOfStrings arrayFlags | |
| // where you declare your flags | |
| flag.Var(&sliceOfStrings, "flag-name", "flag description") | |
| // thisThing -flag-name hello -flag-name world | |
| // |__ sliceOfStrings == []string{"hello", "world"} | |
| // thisThing -flag-name hello,world | |
| // |__ sliceOfStrings == []string{"hello", "world"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment