command option 받는 방법


1. golang 기본 패키지인 flag를 사용하기


http://golang.org/pkg/flag/



package main


import (

"flag"

"fmt"

)


func main() {

var param int

flag.IntVar(&param, "param", 1, "param value")

flag.Parse()


fmt.Printf("param = %d\n", param)

}




결과 

$ go run src/test/test.go --param=100

param = 100

$ go run src/test/test.go 

param = 1



2. 


https://github.com/handlename/go-opts


$ go get github.com/handlename/go-opts


package main


import (

"fmt"


"github.com/handlename/go-opts"

)


type Options struct {

Name string `flag:"name" default:"김용환" description:"이름"`

Age  int    `flag:"age" default:"20" description:"나이"`

}


func main() {

options := Options{}

opts.Parse(&options)

fmt.Println(options)

}


결과

$ go run src/test/test.go --name="이두근" --age=10

{이두근 10}

$ go run src/test/test.go

{김용환 20}





package main


import (

"fmt"


"github.com/handlename/go-opts"

)


type Options struct {

Name string `flag:"param" default:"1" description:"param value"`

}


func main() {

options := Options{}

opts.Parse(&options)

fmt.Println(options)

}



$ go run src/test/test_1.go --param=3

{3}


Posted by '김용환'
,