Shell Script Interfaces
Option Flag
Parse a Mode Switch
Small command-line options let callers change behavior without editing the script body.
Program
Play the script to choose an option and see how the interface maps it to a mode.
option_flag.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
option="--dry-run"
case "$option" in
--dry-run) mode="preview" ;;
--force) mode="apply" ;;
esac
echo "mode=$mode"
#!/usr/bin/env bash
option="--force"
case "$option" in
--dry-run) mode="preview" ;;
--force) mode="apply" ;;
esac
echo "mode=$mode"
option ← --dry-run
3option="--dry-run"4case "$option" invalues this step--dry-runoptioncase "$option" in
3option="--dry-run"4case "$option" in5 --dry-run) mode="preview" ;;values this step--dry-runoptionmode ← preview
4case "$option" in5 --dry-run) mode="preview" ;;6 --force) mode="apply" ;;values this steppreviewmodeecho "mode=$mode"
7esac8echo "mode=$mode"outputmode=previewvalues this steppreviewmode
option ← --force
3option="--force"4case "$option" invalues this step--forceoptioncase "$option" in
3option="--force"4case "$option" in5 --dry-run) mode="preview" ;;values this step--forceoptionmode ← apply
5 --dry-run) mode="preview" ;;6 --force) mode="apply" ;;7esacvalues this stepapplymodeecho "mode=$mode"
7esac8echo "mode=$mode"outputmode=applyvalues this stepapplymode
option
A leading-dash option is a compact caller-facing control.
case parse
`case` maps recognized options to internal behavior.
mode
Using an internal mode word keeps the rest of the script independent of option spelling.