Here's an oversimplified arguments parsing for you so you get the idea:
#!/bin/bash
arg_s=0
arg_d=0
show_help() { printf 'Help me\n'; }
show_version() { printf 'version -∞\n'; }
show_usage() { printf 'Usage: %s [-s|-d|-v|-h] [args...]\n' "${0##*/}"; }
invalid_option() { printf >&2 "you can't use option %s. You dumbo\n" "$1"; show_usage; exit 1; }
while (($#)); do
[[ $1 = -- ]] && { shift; break; }
[[ $1 = -?* ]] || break
case $1 in
(-s) arg_s=1 ;;
(-d) arg_d=1 ;;
(-h) show_help; exit 0 ;;
(-v) show_version; exit 0 ;;
(-*) invalid_option "$1" ;;
esac
shift
done
printf 'You passed the -s flag: %d\n' "$arg_s"
printf 'You passed the -d flag: %d\n' "$arg_d"
printf 'Remaining arguments:\n'
printf ' %s\n' "$@"
Note that it would need some extra work to handle flags like -ds
that means -d -s
1, a little bit of extra work to have options accepting parameters, and some extra work to handle long options. All this is doable without any major problems.
1 you can have a look at my mock which
that has an argument parsing that supports that.