Many command-line tools choose a small action by looking at a subcommand word.

subcommand A subcommand is a word after the program name that selects a mode of work.

Subcommand Dispatch

command
subcommand_dispatch.go
Replay: real traced execution (multi-file project)
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 = "status"
	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 = "init"
	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 = "unknown"
	action := dispatch(command)

	fmt.Println("command=", command)
	fmt.Println("action=", action)
	fmt.Println("known=", action != "show help")
}
  1. command ← "status"

    19func main() {20  var command→ "status" = "status" //@command="init", "unknown"21  action := dispatch(command"status")
  2. handlers ← map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"}

    5func dispatch(command"status" string) string {6  handlers→ map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"} := map[string]string{7    "init":   "create config",8    "status": "show status",9    "sync":   "sync changes",10  }1112  action→ "show status", ok→ true := handlers[command]"show status"13  if !ok {14    return "show help"15  }16  return action"show status"17}
  3. action ← "show status"

    20  var command = "status" //@command="init", "unknown"21  action→ "show status" := dispatch(command"status")2223  fmt.Println("command=", command"status")24  fmt.Println("action=", action"show status")25  fmt.Println("known=", action"show status" != "show help")26}
    outputcommand= status
    action= show status
    known= true
  1. command ← "init"

    19func main() {20  var command→ "init" = "init"21  action := dispatch(command"init")
  2. handlers ← map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"}

    5func dispatch(command"init" string) string {6  handlers→ map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"} := map[string]string{7    "init":   "create config",8    "status": "show status",9    "sync":   "sync changes",10  }1112  action→ "create config", ok→ true := handlers[command]"create config"13  if !ok {14    return "show help"15  }16  return action"create config"17}
  3. action ← "create config"

    20  var command = "init"21  action→ "create config" := dispatch(command"init")2223  fmt.Println("command=", command"init")24  fmt.Println("action=", action"create config")25  fmt.Println("known=", action"create config" != "show help")26}
    outputcommand= init
    action= create config
    known= true
  1. command ← "unknown"

    19func main() {20  var command→ "unknown" = "unknown"21  action := dispatch(command"unknown")
  2. handlers ← map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"}

    5func dispatch(command"unknown" string) string {6  handlers→ map[string]string{"init":"create config", "status":"show status", "sync":"sync changes"} := map[string]string{7    "init":   "create config",8    "status": "show status",9    "sync":   "sync changes",10  }1112  action→ "", ok→ false := handlers[command]""13  if !ok {
  3. if !ok

    12action, ok := handlers[command]13if !okfalse {14  return "show help"15}
  4. action ← "show help"

    20  var command = "unknown"21  action→ "show help" := dispatch(command"unknown")2223  fmt.Println("command=", command"unknown")24  fmt.Println("action=", action"show help")25  fmt.Println("known=", action"show help" != "show help")26}
    outputcommand= unknown
    action= show help
    known= false