I currently have getopts setup the following way:
while getopts ":p:s:d:g:i:h:" opt; do
case ${opt} in
p )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose plex from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
fi
selection="plex"
argument=true
optarg="$OPTARG"
;;
s )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose sonarr from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
fi
selection="sonarr"
argument=true
optarg="$OPTARG"
;;
d )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose deluge from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
fi
selection="deluge"
argument=true
optarg="$OPTARG"
;;
g )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose ip geolocation from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
fi
selection="ip geolocation"
argument=true
optarg="$OPTARG"
;;
i )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose the info page from the command line but supplied an argument" >> $logfolder/advancedplexapi.log
fi
selection="info"
argument=true
optarg="$OPTARG"
;;
h )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose help page for flags from the command line but supplied an argument" >> $logfolder/advancedplexapi.log
fi
echo "The help flag doesn't support an argument"
usage | column -t -s "|"
exit
;;
: )
case "$OPTARG" in
p )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose plex from the command line" >> $logfolder/advancedplexapi.log
fi
selection="plex"
;;
s )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose sonarr from the command line" >> $logfolder/advancedplexapi.log
fi
selection="sonarr"
;;
d )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose deluge from the command line" >> $logfolder/advancedplexapi.log
fi
selection="deluge"
;;
g )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose ip geolocation from the command line" >> $logfolder/advancedplexapi.log
fi
selection="ip geolocation"
;;
i )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose the info page from the command line" >> $logfolder/advancedplexapi.log
fi
selection="info"
;;
h )
if [[ $logging = true ]]
then
echo "$(date '+%d/%m/%Y %H:%M:%S') | info | user chose help page for flags from the command line" >> $logfolder/advancedplexapi.log
fi
usage | column -t -s "|"
exit
;;
esac
;;
\? )
echo "Invalid usage"
usage | column -t -s "|"
exit
;;
esac
done 2>/dev/null
shift $((OPTIND -1))
This code accepts the following:
./script.sh #-> just run the script without any variables set
./script.sh -p #-> selection=plex
./script.sh -s series #-> selection=sonarr & argument=true & optarg=series
./script.sh -h info #-> error because -h flag doens't support arguments
#script can be run barebones (./script.sh)
#script can be run with a flag (only one allowed: -p|-s|-d|-g|-i|-h) (./script.sh -p)
#script can be run with a flag and an argument (argument only allowed when using: -p|-s|-d|-g|-i)(./script.sh -s series) (./script.sh -h info -> not allowed)
My question:
I need to adapt the code to accept a second argument but I really don't know how:
My goal is:
./script.sh -p history list
=
selection=plex
argument=true
optarg=history
second_argument=true
second_optarg=list
./script.sh -p sessions
=
selection=plex
argument=true
optarg=sessions
second_argument=false
second_optarg=
- -p -s and -d are allowed a second argument.
- If an other flag is used with two arguments (a flag that only allows zero or one argument), run
echo "The help flag doesn't support an second argument" && usage | column -t -s "|"
- second_optarg needs to be null byte when no second argument is given
- second_argument needs to be set to false when no second argument is given
- a second argument (only for -p -s and -d ofcourse) isn't required, it's optional
It looks like the user is only allowed to enter a single option: either -p or -s or ... but not any combination. I'd recommend parsing the options without arguments:
Now you can use
"$@"
as you like:or