I've got a Bash function for using a select statement with git checkout. It works fine. I'd like to prevent a use case, and I'm not sure how. How can I prevent non-optarg values being passed to the function as "$1". For example:
$ gco helloshould no-op sincehellois not associated with a-bflag. <-- Need to implement$ gco -b helloshould check out the branchhello. <-- Working$ gcoshould present theselectmenu. <-- Working
As expected, checking for "$1" isn't the answer because it messes with potential optargs.
if [ -z "$1" ]; then
echo "$1"
return
fi
gco () {
local branch=""
while getopts :hb: opt; do
case "$opt" in
h)
echo "NAME: gco - git branch to check out."
echo "SYNOPSYS: gco [-b branch]"
echo "-h Display help text."
echo "-b branch"
return
;;
b) branch="$OPTARG" ;;
:) echo "Missing argument for option -$OPTARG"; return 1 ;;
\?) echo "Unknown option -$OPTARG"; return 1 ;;
esac
done
if [ "$branch" != "" ]; then
git checkout "$branch"
return
fi
echo "Which branch would you like to check out?"
select b in $(git branch | sed 's/* / /'); do
git checkout "$b"
return
done
}
Typical for using getopts is to 'shift' the parsed options after getopts finished. So after your while loop you can do
to remove all parsed options.
OPTINDindicates the next option getopts would check so after the loop it points to the first non-option. As example when calling your function withNow you actually can check for
$1if they are more non-opt args or in your case it is probably enough to