func main() { flags := xflag.NewFlag() name := flags.String("#1", "dolly", "name of object") age := flags.Int("#2", 40, "age of object") flags.ParseArgs() fmt.Println("name", *name, "age", *age) }
func main() { flags := xflag.NewFlag() ints := flags.IntList("#1", "0", "set of ints") flags.ParseArgs() sum := 0 for _, i := range *ints { sum += i } fmt.Println("sum", sum) }
func main() { flag := xflag.NewFlag() f := flag.OpenFile("#1", "stdin", "input file") flag.ParseArgs() fi, e := f.Stat() if e != nil { fmt.Println("error!", e) } else { fmt.Println(fi.IsDirectory(), fi.IsRegular()) } flag.Close() }
func main() { flags := xflag.NewFlag() fl := flags.OpenFileList("#1", "", "input files") flags.ParseArgs() slice := make([]byte, 20) for _, fname := range *fl { fmt.Println("file", fname) f, _ := os.Open(fname) f.Read(slice) fmt.Println(string(slice)) f.Close() } }
// it's possible to use xflags to read a simple property file package main import ( "fmt" "github.com/stevedonovan/xflag" "io/ioutil" ) var config = ` # comment line is ignored name=bonzo dog age=12 # age of animal owners=Alice,John ` var ( flags = xflag.NewFlag() name = flags.String("name", "dolly", "name of object") age = flags.Int("age", 40, "age of object") owners = flags.StringList("owners", "self", "owners of this animal") ) func main() { tmp, _ := ioutil.TempFile("", "tmp") tmp.WriteString(config) tmp.Close() flags.ParseConfig(tmp.Name()) fmt.Printf("name %q %d owners: %v\n", *name, *age, *owners) }