[Golang] Write a simple CLI program
I will show a simple example in this post. Let’s say the program called cli-example. package main import ( "flag" "fmt" "os" ) func main() { command := os.Args[1] os.Args = append(os.Args[:1], os.Args[2:]...) switch command { case "version": fmt.Println("1.1.1") } } The 1st argument will always be the binary name, so the command starts from the 2nd argument. If your app also needs the subcommand, then extract it from the 3rd argument, like docker image ls. ...