- Go ์ธ์ด๋ก ๋ง๋ ๊ฐ๋จํ Argument ํ์ฑ & ํธ๋ค๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋๋ค!
- Short option ๊ธฐ๋ฅ (ex: -a, -b ๋ฑ๋ฑ)
- Help Command ๊ธฐ๋ฅ
- ์ค๋ฅ๋ก๊ทธ ๊ฐ์
- ๋ ์์ธํ ์์ ๋ example๋ก ๊ฐ์๋ฉด ํ์ธ ํ์ค ์ ์์ด์!
- code
package main
import (
"fmt"
"github.com/devproje/commando"
"github.com/devproje/commando/option"
"os"
)
func main() {
// ํ์ผ ์ด๋ฆ์ ์ ์ธํ ์ํ๋ก arguments ์ฃผ์
command := commando.NewCommando(os.Args[1:])
// ์
๋ ฅ argument๊ฐ ์์ ๊ฒฝ์ฐ
command.Root("test", "ํ
์คํธ ๋ช
๋ น์ด ์
๋๋ค!", func(n *commando.Node) error {
fmt.Println("Hello, World!")
return nil
})
// ์
๋ ฅ argument๊ฐ ์์ ๊ฒฝ์ฐ
command.Root("print", "ํ
์คํธ ๋ช
๋ น์ด ์
๋๋ค!", func(n *commando.Node) error {
name, err := option.ParseString(*n.MustGetOpt("name"), n)
if err != nil {
return err
}
fmt.Printf("์
๋ ฅ๋ฐ์ ์ด๋ฆ: %s\n", name)
return nil
})
// ์
๋ ฅ๋ฐ์ arguments๋ฅผ ํ์ฑ ํ ํธ๋ค๋ง
// Execute() ํจ์๋ **๋ฌด์กฐ๊ฑด** Root๋ฅผ ์ค์ ํ ํ์ ์คํ ํด์ผ ํฉ๋๋ค.
err := command.Execute()
if err != nil {
panic(err)
}
}- in terminal
~$ ./sample test
Hello, World!
~$ ./sample print --name "Eungyo Lee"
์
๋ ฅ๋ฐ์ ์ด๋ฆ: Eungyo Lee- code
package main
import (
"fmt"
"github.com/devproje/commando"
"github.com/devproje/commando/option"
"github.com/devproje/commando/types"
"os"
)
func main() {
// ํ์ผ ์ด๋ฆ์ ์ ์ธํ ์ํ๋ก arguments ์ฃผ์
command := commando.NewCommando(os.Args[1:])
command.ComplexRoot("test", "ํ
์คํธ ๋ช
๋ น์ด ์
๋๋ค!", []commando.Node{
command.Then("print", "Hello, World๋ฅผ ์ถ๋ ฅ ํฉ๋๋ค", func(n *commando.Node) error {
fmt.Println("Hello, World!")
return nil
}),
command.Then("sum", "๋ ์์ ํฉ์ ๊ตฌํฉ๋๋ค", func(n *commando.Node) error {
var a, b int64
var err error
a, err = option.ParseInt(*n.MustGetOpt("a"), n)
if err != nil {
return err
}
// MustGetOpt() ํจ์ ๋์ ์๋ Node struct ๋ด๋ถ์ ์๋ Opts ๋ฐฐ์ด๋ก๋ OptionData๋ฅผ ๋ก๋ฉํ๋๊ฒ ๊ฐ๋ฅ ํฉ๋๋ค.
b, err = option.ParseInt(n.Opts[1], n)
if err != nil {
return err
}
fmt.Printf("%d + %d = %d\n", a, b, a + b)
return nil
}, types.OptionData{
Name: "a",
Desc: "์ฒซ๋ฒ์งธ ์ซ์",
Type: types.INTEGER,
}, types.OptionData{
Name: "b",
Desc: "๋๋ฒ์งธ ์ซ์",
Type: types.INTEGER,
}),
})
// ์
๋ ฅ๋ฐ์ arguments๋ฅผ ํ์ฑ ํ ํธ๋ค๋ง
// Execute() ํจ์๋ **๋ฌด์กฐ๊ฑด** Root ๋๋ Complex๋ฅผ ์ค์ ํ ํ์ ์คํ ํด์ผ ํฉ๋๋ค.
err := command.Execute()
if err != nil {
panic(err)
}
}- in terminal
~$ ./sample test print
Hello, World!
~$ ./sample test sum --a 10 --b 20
10 + 20 = 30