Skip to content

Instantly share code, notes, and snippets.

@drm
Created October 11, 2012 10:14
Show Gist options
  • Save drm/3871428 to your computer and use it in GitHub Desktop.
Save drm/3871428 to your computer and use it in GitHub Desktop.
bash: split args & opts
#!/usr/bin/env bash
function a {
echo "let's do A!"
}
function b {
echo "Let's do B!"
}
declare -A OPT
declare -a ARG
for i in $@; do
if [ "${i:0:2}" == "--" ]; then
name=$(echo $i | sed 's/^-\+//g' | awk 'BEGIN { FS="=" } ; {print $1}' )
value=$(echo $i | awk 'BEGIN { FS="=" } ; {print $2}');
if [ "$value" == "" ]; then
value="1"
fi
OPT[$name]=$value
else
ARG[${#ARG[*]}]="$i"
fi;
done;
for i in ${ARG[@]}; do
if [[ $(type -t "$i" 2>/dev/null) == "function" ]]; then
$i;
else
echo "Invalid command $i" >&2
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment