Command-Line Programs
Subcommand Dispatch
Many command-line tools choose a small action by looking at a subcommand word.
Subcommand Dispatch
subcommand_dispatch.go
package main
import "fmt"
func dispatch(command string) string {
handlers := map[string]string{
"init": "create config",
"status": "show status",
"sync": "sync changes",
}
action, ok := handlers[command]
if !ok {
return "show help"
}
return action
}
func main() {
var command =
action := dispatch(command)
fmt.Println("command=", command)
fmt.Println("action=", action)
fmt.Println("known=", action != "show help")
}
package main
import "fmt"
func dispatch(command string) string {
handlers := map[string]string{
"init": "create config",
"status": "show status",
"sync": "sync changes",
}
action, ok := handlers[command]
if !ok {
return "show help"
}
return action
}
func main() {
var command =
action := dispatch(command)
fmt.Println("command=", command)
fmt.Println("action=", action)
fmt.Println("known=", action != "show help")
}
package main
import "fmt"
func dispatch(command string) string {
handlers := map[string]string{
"init": "create config",
"status": "show status",
"sync": "sync changes",
}
action, ok := handlers[command]
if !ok {
return "show help"
}
return action
}
func main() {
var command =
action := dispatch(command)
fmt.Println("command=", command)
fmt.Println("action=", action)
fmt.Println("known=", action != "show help")
}
subcommand
A subcommand is a word after the program name that selects a mode of work.