dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: earnestly <zibeon@googlemail.com>
To: dash@vger.kernel.org
Subject: Re: getopts appears to not be shifting $@ when consuming options
Date: Fri, 29 Jan 2021 22:25:09 +0000	[thread overview]
Message-ID: <YBSLRXjBSbNmOHP2@teapot> (raw)
In-Reply-To: <20210129203650.GA21262@stack.nl>

On Fri, Jan 29, 2021 at 09:36:50PM +0100, Jilles Tjoelker wrote:
> If your actual code is more like:
> 
> set -- -a foo -a bar # for testing
> while getopts :a: arg; do
> ...
> 
> then the script violates the rule I mentioned, and has unspecified
> results.

This, of course, would be the intended use of such a technique.

Having read the a little more of the standard I may have come up with a
solution which both appears to work and without much ugliness.  I hope
it also doesn't violate either the spirit or word of the standard on this
matter:

    #!/bin/sh --

    while getopts :a: arg; do
        case $arg in
            a) set -- "$@" attr="$OPTARG"; shift "$((OPTIND - 1))"; OPTIND=1
        esac
    done
    shift "$((OPTIND - 1))"

It would be quite sad for me to lose this functionality.

P.S. To my amusement this works with bourne as well but shifting needs
     to be undertaken with `expr "$OPTIND" - 1` instead.

  parent reply	other threads:[~2021-01-29 22:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-29 18:25 getopts appears to not be shifting $@ when consuming options earnestly
2021-01-29 20:15 ` Harald van Dijk
2021-01-30 15:31   ` Harald van Dijk
2021-01-29 20:36 ` Jilles Tjoelker
2021-01-29 21:19   ` Harald van Dijk
2021-01-29 22:25   ` earnestly [this message]
2021-01-30  6:39     ` Vladimir N. Oleynik
2021-01-30  7:36     ` Vladimir N. Oleynik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YBSLRXjBSbNmOHP2@teapot \
    --to=zibeon@googlemail.com \
    --cc=dash@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).