help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Handling short and long options


From: angioberlinguer
Subject: Handling short and long options
Date: Mon, 06 Sep 2021 21:35:01 +0000

I have made the following function to parse short and long options. Perhaps I 
do not need to
call `set -- $*`.

The while loop could also be changed. I want to allow splitting on space and 
equal sign
so I can call it with `-s val` and `--src=val`.

rando ()
{

local incl=() fls=()
IFSPREV="$IFS" # Save IFS (splits arguments on whitespace by default)
IFS=" =" # Split arguments on " " and "="
set -- $* # Set positional parameters to command line arguments
IFS="$IFSPREV" # Set original IFS

local iarg=0 narg="$#"
while (( narg > 0 )); do
opt="$1"
iarg=$(( iarg + 1 ))
case $opt in
("--incl") incl+=("$2") ; shift 2 ;;
("-s"|"--src"|"--source") src=$( "$2" ) ; shift 2 ;;
("-d"|"--dst"|"--destin") dst="$2" ; shift 2 ;;
("--") shift 1 ; break ;;
("-"*) printf '%s\n' "Unknown option: $1" ; shift 1 ;;
(*) fls+=( "$2" ) ; shift 1 ;;
esac
done

reply via email to

[Prev in Thread] Current Thread [Next in Thread]