How do I take a string and parse it as if it was a series of command line arguments? I want to be able to take a string and parse that with flag and/or pflag. How do I do this?
package main
import (
"fmt"
"os"
flag "github.com/spf13/pflag"
)
func main() {
command := `unimportant -fb "quoted string"`
var (
foo bool
bar string
)
flag.BoolVarP(&foo, "foo", "f", false, "foo")
flag.StringVarP(&bar, "bar", "b", "default", "bar")
// Parse command
os.Args = append(os.Args, "-fb", "quoted string")
flag.Parse()
fmt.Println(command)
fmt.Println("foo", foo)
fmt.Println("bar", bar)
}