help-bash
[Top][All Lists]
Advanced

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

getopt optional argument with optional value


From: Khan Smith
Subject: getopt optional argument with optional value
Date: Mon, 11 Oct 2021 01:23:42 +0200

   I am using getopt and want to have an option -l that takes an optional
   value. I am finding that when matching -l by itself with no argument
   value provided, I still have to use shift 2 rather than just one shift.
   What is going on and how does this happen?
opts=$( getopt -o "$shortopts" -l "$longopts" -n "${0##*/}" -- "$@" )
eval "set -- ${opts}"
while (( $# > 0 )); do
  case $1 in
    ("-l")
      case "$2" in
        (+([[:digit:]]))
          # matches -lNUM, optional argument value
          nl="$2"; shift 2 ;;
        (*)
          # matches -l, no argument value provided
          nl=1 ; shift ;;
        esac
        ;;
      ("--") shift ; break ;;
      (*) opt_error=1 ; break ;;
    esac
  done


reply via email to

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