Skip to content

Instantly share code, notes, and snippets.

@tecnologer
Created June 16, 2019 20:27
Show Gist options
  • Select an option

  • Save tecnologer/841a611e5a891aa853f089adbfdd3b40 to your computer and use it in GitHub Desktop.

Select an option

Save tecnologer/841a611e5a891aa853f089adbfdd3b40 to your computer and use it in GitHub Desktop.
Ejemplo de uso de `flag` para parsear parametros de un string
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