zsh-workers
 help / color / mirror / code / Atom feed
From: Roman Neuhauser <neuhauser@sigpipe.cz>
To: Peter Stephenson <p.stephenson@samsung.com>
Cc: zsh-workers@zsh.org
Subject: Re: getopts doesn't update OPTIND when called from function
Date: Fri, 5 Jun 2015 08:46:48 +0200	[thread overview]
Message-ID: <20150605064648.GE15174@isis.sigpipe.cz> (raw)
In-Reply-To: <20150528181740.20267791@pwslap01u.europe.root.pri>

# p.stephenson@samsung.com / 2015-05-28 18:17:40 +0100:
> On Thu, 28 May 2015 18:18:32 +0200
> Martijn Dekker <martijn@inlv.org> wrote:
> > I'm writing a shell function that extends the functionality of the
> > 'getopts' builtin. For that to work, it is necessary to call the
> > 'getopts' builtin from the shell function.
> > 
> > The POSIX standard specifies that OPTIND and OPTARG are global
> > variables, even if the positional parameters are local to the
> > function.[*] This makes it possible to call 'getopts' from a function by
> > simply passing the global positional parameters along by adding "$@".
> > 
> > My problem is that zsh does not update the global OPTIND variable when
> > getopts is called from a function, which defeats my function on zsh. (It
> > does update the global OPTARG variable, though.)
> 
> This is documented behaviour (well, sort of -- the documentation didn't
> say quite what we actually do) so this needs another compatibility fix.
> POSIX_BUILTINS seems appropriate.

thanks for the zshbuiltins(1) clarification.  

i have a problem similar to Martijn's, only there's no function in sight.
this diff shows how zsh (5.0.7, emulate -R sh) differs from other shells,
and the standard IIUC:

   $ getopts abc OPTNAM -a -b -- -c
   $ printf "%s=%s\n" OPTIND "$OPTIND" OPTNAM "$OPTNAM" OPTARG "$OPTARG"
-  OPTIND=2
+  OPTIND=1
   OPTNAM=a
   OPTARG=
   $ getopts abc OPTNAM -a -b -- -c
   $ printf "%s=%s\n" OPTIND "$OPTIND" OPTNAM "$OPTNAM" OPTARG "$OPTARG"
-  OPTIND=3
+  OPTIND=2
   OPTNAM=b
   OPTARG=
   $ getopts abc OPTNAM -a -b -- -c
   [1]
   $ printf "%s=%s\n" OPTIND "$OPTIND" OPTNAM "$OPTNAM" OPTARG "$OPTARG"
   OPTIND=4
-  OPTNAM=?
+  OPTNAM=b
   OPTARG=

it would be nice to have zsh aligned with other shells, at least under
posix_builtins or 'emulate sh'.

-- 
roman


  reply	other threads:[~2015-06-05  6:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-28 16:18 Martijn Dekker
2015-05-28 17:17 ` Peter Stephenson
2015-06-05  6:46   ` Roman Neuhauser [this message]
2015-06-06 16:46     ` Bart Schaefer
2015-06-07  8:30       ` Roman Neuhauser

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=20150605064648.GE15174@isis.sigpipe.cz \
    --to=neuhauser@sigpipe.cz \
    --cc=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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