CLI 인자를 관리할 수 있는 패키지 Cobra 활용
$ go install github.com/spf13/cobra-cli@latest
$ go mod init goproj
go: creating new go.mod: module goproj
$ cobra-cli init -a w00j00ng351@gmail.com
Your Cobra application is ready at
/go/src/goproj
명령 수행 후 main.go
, LICENSE
, go.sum
, cmd/root.go
파일이 생성되고, go.mod
파일에 종속성 정보가 추가됨
main.go
파일내용 확인
/*
Copyright © 2022 w00j00ng351@gmail.com
*/
package main
import "goproj/cmd"
func main() {
cmd.Execute()
}
cmd/root.go
파일내용 확인/*
Copyright © 2022 w00j00ng351@gmail.com
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "goproj",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.goproj.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
$ cobra-cli add serve -a w00j00ng351@gmail.com
serve created at /home/player/source/private/goproj
cmd/serve.go
파일 추가됨/*
Copyright © 2022 w00j00ng351@gmail.com
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called")
},
}
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
$ go run main.go
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.
Usage:
goproj [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
serve A brief description of your command
Flags:
-h, --help help for goproj
-t, --toggle Help message for toggle
Use "goproj [command] --help" for more information about a command.
serve
인자를 입력하고 프로그램 호출$ go run main.go serve
serve called