Last active
June 1, 2016 08:18
-
-
Save awalterschulze/1e4711db65895201d35f0d25fc4a36af to your computer and use it in GitHub Desktop.
golang flag library
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
package "main" | |
import "somelibrary" | |
import "flags" | |
func main() { | |
someFlag := somelibrary.Impls.Flag() | |
flags.Parse() | |
someImpl := somelibrary.GetImpl(someFlag) | |
run(someImpl) | |
} |
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
package somelibrary | |
type Flag struct { | |
param string | |
def string | |
desc string | |
} | |
var Impls = Flag{"some", "real", "Some Type"} | |
func (this Flag) Flag() *string { | |
return flag.String(this.param, this.def, this.desc+" "+implUsage()) | |
} | |
var impls = map[string]impl{ | |
"mock": mockImpl, | |
"real": realImpl, | |
} | |
func implUsage() string { | |
ss := make([]string, 0, len(impls)) | |
for i := range impls { | |
ss = append(ss, i) | |
} | |
return "(" + strings.Join(ss, "|") + ")" | |
} | |
func GetImpl(impl string) impl { | |
return impls[impl] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment