forked from octago/sflags
/
main.go
48 lines (42 loc) · 869 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
// This packages shows how to use sflags with flag library.
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/octago/sflags/gen/gflag"
)
type httpConfig struct {
Host string
}
type config struct {
httpConfig
}
func main() {
cfg := &config{
httpConfig: httpConfig{
Host: "127.0.0.1",
},
}
// Use gflags.ParseToDef if you want default `flag.CommandLine`
fs, err := gflag.Parse(cfg)
if err != nil {
log.Fatalf("err: %v", err)
}
fs.Init("", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
// You should run fs.Parse(os.Args[1:]), but this is an example.
err = fs.Parse([]string{
"-http-config-host", "localhost",
})
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Println("Usage:")
fs.SetOutput(os.Stdout)
fs.PrintDefaults()
fmt.Printf("cfg: %s\n", spew.Sdump(cfg))
}