Created
June 16, 2019 20:27
-
-
Save tecnologer/841a611e5a891aa853f089adbfdd3b40 to your computer and use it in GitHub Desktop.
Ejemplo de uso de `flag` para parsear parametros de un string
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 ( | |
| "flag" | |
| "fmt" | |
| "strings" | |
| ) | |
| type flagT string | |
| //implementacion de interface flag.Value | |
| func (f *flagT) String() string { | |
| return string(*f) | |
| } | |
| func (f *flagT) Set(v string) error { | |
| *f = flagT(v) | |
| return nil | |
| } | |
| var ( | |
| a flagT | |
| b flagT | |
| c flagT | |
| ) | |
| func main() { | |
| input := "-a 1 -b 2 -c 3" | |
| args := strings.Split(input, " ") | |
| /* | |
| ContinueOnError ErrorHandling = iota // Return a descriptive error. | |
| ExitOnError // Call os.Exit(2). | |
| PanicOnError // Call panic with a descriptive error. | |
| */ | |
| commandLine := flag.NewFlagSet("test", flag.ContinueOnError) | |
| commandLine.Var(flag.Value(&a), "a", "flag llamada a") | |
| commandLine.Var(flag.Value(&b), "b", "flag llamada b") | |
| commandLine.Var(flag.Value(&c), "c", "flag llamada c") | |
| commandLine.Parse(args) | |
| fmt.Printf("a=%s, b=%s, c=%s", a, b, c) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment