Created
September 29, 2023 21:39
-
-
Save konsolebox/985da7c3442ce5c5c792928f65c3a7cd to your computer and use it in GitHub Desktop.
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
| function get_opt_and_optarg_gnu { | |
| local optional=false | |
| if [[ $1 == @optional ]]; then | |
| optional=true | |
| shift | |
| fi | |
| OPT=$1 OPTARG= OPTSHIFT=0 | |
| if [[ $1 == -[!-]* && ${optional} == true ]]; then | |
| OPT=${1:0:2} OPTARG=${1:2} | |
| [[ ${OPTARG} ]] || return 1 | |
| elif [[ $1 == -[!-]?* ]]; then | |
| OPT=${1:0:2} OPTARG=${1:2} | |
| elif [[ $1 == --*=* ]]; then | |
| OPT=${1%%=*} OPTARG=${1#*=} | |
| elif [[ ${2+.} && ${optional} == false ]]; then | |
| OPTARG=$2 OPTSHIFT=1 | |
| elif [[ $1 == --* && ${optional} == true ]]; then | |
| return 1 | |
| else | |
| echo "No argument specified for '$1'." | |
| exit 2 | |
| fi | |
| return 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment