help-bash
[Top][All Lists]
Advanced

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

getopts does not support optional arguments to options


From: nigelberlinguer
Subject: getopts does not support optional arguments to options
Date: Fri, 27 Aug 2021 14:33:05 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, August 27, 2021 1:04 AM, nigelberlinguer 
<nigelberlinguer@protonmail.com> wrote:

> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Friday, August 27, 2021 12:07 AM, Andreas Kusalananda Kähäri 
> andreas.kahari@abc.se wrote:
>
> > On Thu, Aug 26, 2021 at 11:47:24PM +0000, nigelberlinguer wrote:
> >
> > > Sent with ProtonMail Secure Email.
> > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > > On Thursday, August 26, 2021 11:42 PM, Andreas Kusalananda Kähäri 
> > > andreas.kahari@abc.se wrote:
> > >
> > > > On Thu, Aug 26, 2021 at 09:58:58PM +0000, nigelberlinguer via wrote:
> > > >
> > > > > -   Have been told that getopts does not support optional arguments 
> > > > > to options. Either you declare the option with : (and failing to 
> > > > > provide an argument is an error); or you don't (and providing an 
> > > > > argument is an error). Is this true, with no workarounds ? Then I 
> > > > > have to write the parsing myself, right ?.
> > > >
> > > > getopt is not a bash thing (like e.g. the built-in getopts is). It's an
> > > > external utility provided by some other software package on your system.
> > > > On GNU systems, this is usually provided by a "util-linux" package,
> > > > while BSD systems have a different and incompatible variant in their
> > > > base system.
> > > > You will find an example of the use of the GNU getopt utility that
> > > > includes optional option-arguments in the "util-linux" source
> > > > distribution:
> > > > https://salsa.debian.org/debian/util-linux/-/blob/master/misc-utils/getopt-example.bash
> > >
> > > Was meaning to say getopts.
> >
> > I know.
> > Here's from today on this list (no replies yet):
> > https://mail.gnu.org/archive/html/help-bash/2021-08/msg00190.html
> > Parts of this thread also deals with this:
> > https://mail.gnu.org/archive/html/help-bash/2021-08/msg00050.html
> > This recent thread also starts out talking about getopt, bet then
> > touches on getopts and the issue of optional option-arguments:
> > https://mail.gnu.org/archive/html/help-bash/2021-07/msg00163.html
> > The answers to the following StackExchange issue did not get much love
> > from the community, but may contain some code that you could use (with
> > attribution, obviously):
> > https://unix.stackexchange.com/questions/663803/can-you-make-a-bash-scripts-option-arguments-be-optional
>
> Am looking at the stack exchange one. I need to study it well though as it
> is not so straightforward to understand.

Does an option with optional argument involve putting a case statement, using a 
pattern (word)
to match another option starting with `-` and (*) te set the user defined 
option?

I do not understand the `shift` `OPTIND`, and `set` parts.  What is happening 
there?

case $opt in
  ("a")
    case $OPTARG in
      (-*)
      shift "$(( OPTIND - 1 ))"
      OPTIND=1
      set -- "$OPTARG" "$@"
      a_opt=$a_default
      ;;
    *)
      a_opt=$OPTARG   # user defined optional argument
    esac
    ;;





reply via email to

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