Skip to content

Latest commit

ย 

History

History
129 lines (110 loc) ยท 3.08 KB

File metadata and controls

129 lines (110 loc) ยท 3.08 KB

Commando

GoDoc Build Status

  • Go ์–ธ์–ด๋กœ ๋งŒ๋“  ๊ฐ„๋‹จํ•œ Argument ํŒŒ์‹ฑ & ํ•ธ๋“ค๋ง ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค!

Milestone

- Short option ๊ธฐ๋Šฅ (ex: -a, -b ๋“ฑ๋“ฑ)

  • Help Command ๊ธฐ๋Šฅ
  • ์˜ค๋ฅ˜๋กœ๊ทธ ๊ฐœ์„ 

How to use

  • ๋” ์ž์„ธํ•œ ์˜ˆ์ œ๋Š” example๋กœ ๊ฐ€์‹œ๋ฉด ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์–ด์š”!

General

  • 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

Complex

  • 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