A larger script can keep argument routing separate from the functions that do real work. The dispatch table maps a command to a handler.

Program

Play the script to choose the command and see the selected handler.

command
main_dispatch_plan.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

command="build"
case "$command" in
    build) handler="run_build" ;;
    clean) handler="run_clean" ;;
esac
echo "$command -> $handler"
#!/usr/bin/env bash

command="clean"
case "$command" in
    build) handler="run_build" ;;
    clean) handler="run_clean" ;;
esac
echo "$command -> $handler"
  1. command ← build

    3command="build"4case "$command" in
    values this stepbuildcommand
  2. case "$command" in

    3command="build"4case "$command" in5    build) handler="run_build" ;;
    values this stepbuildcommand
  3. handler ← run_build

    4case "$command" in5    build) handler="run_build" ;;6    clean) handler="run_clean" ;;
    values this steprun_buildhandler
  4. echo "$command -> $handler"

    7esac8echo "$command -> $handler"
    outputbuild -> run_build
    values this stepbuildcommandrun_buildhandler
  1. command ← clean

    3command="clean"4case "$command" in
    values this stepcleancommand
  2. case "$command" in

    3command="clean"4case "$command" in5    build) handler="run_build" ;;
    values this stepcleancommand
  3. handler ← run_clean

    5    build) handler="run_build" ;;6    clean) handler="run_clean" ;;7esac
    values this steprun_cleanhandler
  4. echo "$command -> $handler"

    7esac8echo "$command -> $handler"
    outputclean -> run_clean
    values this stepcleancommandrun_cleanhandler
dispatch Dispatch selects the handler for a named command.
handler A handler function contains the command-specific work.
main function A main function can parse, dispatch, and report without mixing every detail together.