Skip to content

Instantly share code, notes, and snippets.

@robinbowes
Last active July 9, 2020 13:17
Show Gist options
  • Save robinbowes/e3e656736f0421985173091b41f7998b to your computer and use it in GitHub Desktop.
Save robinbowes/e3e656736f0421985173091b41f7998b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
main() {
parse_cmdline "$@"
}
# parse the command line
parse_cmdline() {
_ARGS[fix]=0
_ARGS[prompt]=1
_ARGS[extra]=''
local argv
argv=$(getopt -o 'efp:' --long 'extra:,fix,prompt' -- "$@") || return
echo "[$argv]"
eval "set -- $argv"
echo "\$@: " "$@"
for argv; do
echo "argv: $argv"
echo "\$1: ${1:-undef}"
echo "\$2: ${2:-undef}"
case $argv in
-f | --fix)
_ARGS[fix]=1
shift
;;
-p | --prompt)
_ARGS[prompt]=1
shift
;;
-e | --extra)
_ARGS[extra]="$2"
shift 2
;;
--)
shift
break
;;
*)
echo 'Something went wrong processing command line options' >&2
exit 2
;;
esac
done
echo "Fix: ${_ARGS[fix]}"
echo "Prompt: ${_ARGS[prompt]}"
echo "Extra: ${_ARGS[extra]}"
echo "Files: ${_FILES[*]}"
}
initialize() {
# get directory containing this script
local dir
local source
source="${BASH_SOURCE[0]}"
while [[ -h $source ]]; do # resolve $source until the file is no longer a symlink
dir="$( cd -P "$( dirname "$source" )" >/dev/null && pwd )"
source="$(readlink "$source")"
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $source != /* ]] && source="$dir/$source"
done
_SCRIPT_DIR="$(dirname "$source")"
# source getopt function
# shellcheck source=getopt.bash
. "$_SCRIPT_DIR/getopt.bash"
}
initialize
# declare global variables
declare -A _ARGS
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment