У меня в настоящее время есть Getopts Setup следующим образом:
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))
Этот код принимает следующее:
./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 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=
ECHO «Флаг справки не поддерживает второй аргумент» && Использование | Column -T -S «|»
Похоже, что пользователю разрешено вводить только одну опцию: либо -p, либо -s, либо ... но не любую комбинацию. Я бы порекомендовал разбирать опции без аргументов :
selections=()
while getopts ":psdgih" opt; do
case ${opt} in
p) selections+=("plex") ;;
s) selections+=("sonarr") ;;
d) selections+=("deluge") ;;
g) selections+=("ip geolocation") ;;
i) selections+=("info") ;;
h) show_help; exit ;;
\?) echo "Invalid usage"
usage | column -t -s "|"
exit
;;
esac
done 2>/dev/null
case ${#selections[@]} in
0) selection="default case" ;;
1) selection=${selections[0]} ;;
*) echo "only one selection allowed" >&2; exit 1 ;;
esac
shift $((OPTIND - 1))
Теперь можно использовать "$@"
как угодно:
case $# in
0) echo "some error" >&2; exit 1 ;;
1) optarg=$1
second_argument=false
;;
*) optarg=$1
second_argument=true
second_optarg=$2
;;
esac
или
case $1 in
history)
case $2 in
list) : ...
;;