help-bash
[Top][All Lists]
Advanced

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

Re: getopt optional argument with optional value


From: Dennis Williamson
Subject: Re: getopt optional argument with optional value
Date: Sun, 10 Oct 2021 21:12:52 -0500

On Sun, Oct 10, 2021, 7:41 PM Khan Smith <khansmith@mail.com> wrote:

>    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
>

Here we go again.

getopt is not Bash. For help with getopt, try mailing lists or forums that
are devoted to your operating system or distribution. Much advice you will
see in various places says not to use getopt. Consider following that
advice. getopt has implementations with bugs.

Bash has a feature called getopts - note the s. It doesn't support long
options. It works consistently though. It supports flags with arguments and
flags without.

As for "optional" options - how do you suppose that a flag followed by no
option then another flag could be distinguished from a flag followed by an
option that looks like a flag?

>


reply via email to

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