Last active
May 2, 2022 14:38
-
-
Save konsolebox/a430028795bec766aba6ffddd0b5d12b to your computer and use it in GitHub Desktop.
This file contains 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 { | |
local optional=false | |
if [[ $1 == @optional ]]; then | |
optional=true | |
shift | |
fi | |
OPT=$1 OPTARG= OPTSHIFT=0 | |
if [[ $1 == -[!-]?* ]]; then | |
OPT=${1:0:2} OPTARG=${1:2} | |
elif [[ $1 == --*=* ]]; then | |
OPT=${1%%=*} OPTARG=${1#*=} | |
elif [[ ${2+.} && (${optional} == false || $2 != -?*) ]]; then | |
OPTARG=$2 OPTSHIFT=1 | |
elif [[ ${optional} == true ]]; then | |
return 1 | |
else | |
fail "No argument specified for '$1'." | |
fi | |
return 0 | |
} | |
if false; then | |
# No optional version | |
function get_opt_and_optarg { | |
OPT=$1 OPTARG= OPTSHIFT=0 | |
if [[ $1 == -[!-]?* ]]; then | |
OPT=${1:0:2} OPTARG=${1:2} | |
elif [[ $1 == --*=* ]]; then | |
OPT=${1%%=*} OPTARG=${1#*=} | |
elif [[ ${2+.} ]]; then | |
OPTARG=$2 OPTSHIFT=1 | |
else | |
fail "No argument specified for '$1'." # Or simply return 1 | |
fi | |
return 0 | |
} | |
fi | |
function main { | |
... | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-[eIX]*) | |
get_opt_and_optarg "${@:1:2}" | |
shift "${OPTSHIFT}" | |
[[ ${OPTARG} ]] || fail "Argument for '${OPT}' can't be empty." | |
expressions+=("${OPTARG}") | |
expr_opts+=("${OPT}") | |
if [[ ${2-} == @* ]]; then | |
flags=${2:1} invalid_flags=${flags//[EFGPliw]} | |
[[ ${invalid_flags} ]] && fail "Invalid flags specified: ${invalid_flags}" | |
expr_flags+=("${flags}") | |
shift | |
else | |
expr_flags+=('') | |
fi | |
;; | |
-E|--extended-regexp) | |
... | |
-[cmnps]*|--@(bytes|max-unchanged-stats|lines|pid|sleep-interval)?(=*)) | |
get_opt_and_optarg "${@:1:2}" | |
shift "${OPTSHIFT}" | |
[[ ${OPTARG} ]] || fail "Argument for '${opt}' can't be empty." | |
... | |
tail_opts+=("${OPT}" "${OPTARG}") | |
;; | |
... | |
-L*|--limit-final?(=*)) | |
final_limit=10 timeout=1 | |
if get_opt_and_optarg @optional "${@:1:2}"; then | |
shift "${OPTSHIFT}" | |
if [[ ${OPTARG} == *,* ]]; then | |
final_limit=${OPTARG%%,*} timeout=${OPTARG#*,} | |
else | |
final_limit=${OPTARG} | |
fi | |
... | |
fi | |
... | |
;; | |
... | |
--) | |
files+=("${@:2}") | |
break | |
;; | |
-[!-][!-]*) | |
set -- "${1:0:2}" "-${1:2}" "${@:2}" | |
continue | |
;; | |
-?*) | |
fail "Invalid option: $1" | |
;; | |
*) | |
files+=("$1") | |
;; | |
esac | |
shift | |
done | |
... | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment