Last active
July 17, 2018 02:24
-
-
Save gocha/8c74f9724bfb9c1e3a98b28bd5cceddc to your computer and use it in GitHub Desktop.
Sample for ps_getopt.sh: Powershell-flavored command line argument parser
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 | |
# | |
# Powershell-flavored command line argument parser example. | |
# | |
# Requires: ps_getopt.sh <https://gist.github.com/gocha/9b12fa0b7b5d18ae56e0452cec749e87> | |
# License: Unlicensed | |
. ps_getopt.sh | |
ALL_PARAMS=("Path" "Nullable" "Optional" "Force") | |
ORDINAL_PARAMS=("Path" "Nullable" "Optional") | |
MANDATORY_PARAMS=("Path" "Nullable") | |
EMPTY_ALLOWED_PARAMS=("Nullable") | |
SWITCH_TYPE_PARAMS=("Force") | |
main() { | |
if [[ "$1" == "--help" ]]; then | |
help | |
return 1 | |
fi | |
ps_getopt ALL_PARAMS[@] ORDINAL_PARAMS[@] MANDATORY_PARAMS[@] EMPTY_ALLOWED_PARAMS[@] SWITCH_TYPE_PARAMS[@] "$@" || return 1 | |
# Show given parameters. | |
echo "Parameters" | |
echo "----------" | |
echo | |
for name in "${!params[@]}"; do | |
echo "${name}: ${params[${name}]}" | |
done | |
# Example branch for a switch parameter. | |
if [[ ! -z ${params["Force"]} ]]; then | |
echo | |
echo "Force is with you." | |
fi | |
return 0 | |
} | |
help() { | |
echo "Show help page." | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment