Regular Expressions 101

Community Patterns

Parse getopt options from a Bash script

0

Regular Expression
PCRE (PHP <7.3)

/
getopt(?:s|)(?: (?:-[a-zA-Z])*)*(?'name'[$a-zA-Z]+|)\s(?:(?:-o|--options) (?'short'.+) (?:-l|--longoptions|--long) (?'long'(?:[a-zA-Z0-9]|,|:|\$)+) \-\- \"\$@\"|(?:(?:(?:-[a-zA-Z])*|)\s*\"(?'while'(?:[a-zA-Z0-9]|,|:|\$)+)\"))
/
gm

Description

This will parse the parameters/arguments that a Bash script when it uses getopt(s).

Groups: name,short,long,while

Example cases:

while getopts -a ":n:t:" options; do
while getopts -a -n ":n:t:" options; do
while getopt ":n:t:" options; do
Parameters=$(getopt -a -n $ScriptName --options $SHORT --longoptions $LONG -- "$@")
Parameters=$(getopt -a -n alphabet -o abc:d: --long alpha,bravo,charlie:,delta: -- "$@")
Submitted by Spyingwind - a year ago