Created
May 2, 2025 11:47
-
-
Save elPytel/b006352dae16e8f1f577e19f75bd05d3 to your computer and use it in GitHub Desktop.
Bash arg-parse FSM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# By Pytel | |
#DEBUG=true | |
DEBUG=false | |
VERBOSE=false | |
function printHelp () { | |
echo -e "USE:" | |
echo -e " $(echo "$0" | tr "/" "\n" | tail -n 1) -f [atribut] COMMANDs" | |
echo "" | |
echo -e "COMMANDS:" | |
echo -e " -h --help \t print this text" | |
echo -e " -d --debug\t enable debug output" | |
echo -e " -v --verbose\t enable vebrose mode" | |
echo -e " -f --func\t run function" | |
echo -e " -t --time \t set spin up time" | |
} | |
$DEBUG && echo "Args: [$*]" | |
# input args | |
case $# in | |
0) printHelp; exit 2;; | |
*) arg=$1;; | |
esac | |
# parse input | |
while [ $# -gt 0 ] ; do | |
$DEBUG && echo "Arg: $arg remain: $#" | |
# vyhodnoceni | |
case $arg in | |
-h | --help) printHelp; exit 2;; | |
-d | --debug) DEBUG=true;; | |
-v | --verbose) VERBOSE=true;; | |
-f | --func) shift; runFunc "$1" || exit 3;; | |
-t | --time) shift; setTime "$1" || exit 4;; | |
*) echo -e "Unknown parametr: $arg"; exit 1;; | |
esac | |
# next arg | |
shift | |
arg=$1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment