zsh-users
 help / color / mirror / code / Atom feed
* zparesopts odd behaviour
@ 2021-09-02 18:26 Roger Mason
  2021-10-27 21:22 ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Roger Mason @ 2021-09-02 18:26 UTC (permalink / raw)
  To: zsh-users

Hello,

Given this script:

#!/usr/local/bin/zsh -f
ELKVER=elk-7.1.14

floats=($(awk '/!     default values     !/{flag=1;next}/!     read from elk.in     !/{flag=0}flag' \
~/Software/Elk/$ELKVER/src/readinput.f90 \
| awk '!/!/' | awk '!/^if/'| awk '!/^allocate/' | awk '!/:/' \
| awk '{gsub(/ /,""); print}' | sed 's/[(]/_/g;s/:/_/g;s/)//g;s/,_/all_/g;s/,\([0-9]\)/_\1/g' | sort \
| awk '{gsub(/=/," "); print}' | awk '$2 ~ /[0-9]+(\.)[0-9]*/ {print "-"$1":"}'))

echo "$floats"

#zparseopts -D -E -A elkfloats - -sc: -sc1: -sc2: -sc3:
zparseopts -D -E -A elkfloats - ${floats}
OPTSELK=""

for key value in "${(@kv)elkfloats}"; do
    k=$(echo $key | tr -d '-')
    ${k}=${elkfloats[$key]} 2>/dev/null # Discard error message: command not found:
    if [[ $k == sc* ]];then		# sc is used internally in the elk code vs elk.in.
	k=$(echo $k | sed 's/sc/scale/g') # Should work on sc1, sc2 & sc3.
#	echo "$k = $value"
    fi
      OPTSELK+="$k\n $value\n\n"
done
echo "$OPTSELK"
exit 10

Called like this:

./testopts.sh  --sc1 5.5 --sc2 5.5 --sc3 8.0 --sc 1.5

I get this output:

-afindscf: -amixpm_1: -amixpm_2: -avec_1_1: -avec_2_2: -avec_3_3: -avecu_1_1: -avecu_2_2: -avecu_3_3: -befvit: -broydpm_1: -broydpm_2: -c_tb09: -chgexs: -deapwlo: -deltaem: -deltaph: -deltast: -demaxbnd: -dengy0: -dlefe: -dncgga: -dtimes: -ecvcut: -ehfb: -emaxelnes: -emaxrf: -ephscf_1: -ephscf_2: -epsband: -epschg: -epsdev: -epsefm: -epsefvit: -epsengy: -epsforce: -epslat: -epsocc: -epspot: -epsstress: -esccut: -evaltol: -fracinr: -fxclrc_1: -fxclrc_2: -gmaxrf: -gmaxvr: -hkmax: -hmaxvr: -hybridc: -mstar: -mustar: -nempty0: -nrmtscf: -q0cut: -radkpt: -rdmalpha: -rdmtemp: -reducebf: -rgkmax: -rmtall: -rmtdelta: -rndatposc: -rndavec: -rndbfcmt: -rndbfcu: -rndevt0: -sc1: -sc2: -sc3: -sc: -scissor: -scu1: -scu2: -scu3: -scu: -socscf: -solscf: -sqados_3: -swidth: -sxcscf: -t0tdlr: -tau0atp: -tau0latv: -tauefm: -taufsm: -tauftm: -tauoep_1: -tauoep_2: -taurdmc: -taurdmn: -tdphi: -thetamld: -trial_step: -tstime: -vclp2d_1_1: -vclp2d_2_2: -vclp3d_1_1: -vclp3d_2_2: -vclp3d_3_3: -vhmat_1_1: -vhmat_2_2: -vhmat_3_3: -wmaxgw: -wphcut: -wplot_1: -wplot_2: -wsfac_1: -wsfac_2:
scale
 1.5

The output I expect is:

scale
 1.5

scale1
 5.5

scale2
 5.5

scale3
 8.0

The option names are extracted from variable names in a piece of fortran
code.  The line 'echo floats' in the script is just for debugging, it
verifies that the options are being parsed correctly from that code.

If I call the script thus:

./testopts.sh  --sc1 5.5 --sc2 5.5 --sc3 8.0

the output (without the debugging line) is:

scale
 3

When called thus:

./testopts.sh  --sc1 5.5 --sc2 5.5

I get

scale
 2

It looks like zparseopts is confusing the option and the option
argument, but perhaps something else is going on.

I'm running zsh-5.8 on FreeBSD 11.4.

I appreciate any help in getting this to work correctly.

Thanks,
Roger


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: zparesopts odd behaviour
  2021-09-02 18:26 zparesopts odd behaviour Roger Mason
@ 2021-10-27 21:22 ` Oliver Kiddle
  2021-10-28  0:05   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2021-10-27 21:22 UTC (permalink / raw)
  To: Roger Mason; +Cc: zsh-users

On 2 Sep, Roger Mason wrote:
> ./testopts.sh  --sc1 5.5 --sc2 5.5 --sc3 8.0 --sc 1.5

zparseopts allows values to appear immediately after the option. So
--sc3 is taken as being the same as --sc 3
So it thinks you have four --sc options and only the last is kept.

zparseopts was designed for splicing up options to completion functions
where it is common to change them before passing a subset on to other
functions. It may not be ideally suited to other situations.

Oliver


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: zparesopts odd behaviour
  2021-10-27 21:22 ` Oliver Kiddle
@ 2021-10-28  0:05   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2021-10-28  0:05 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Roger Mason, Zsh Users

On Wed, Oct 27, 2021 at 2:22 PM Oliver Kiddle <opk@zsh.org> wrote:
>
> On 2 Sep, Roger Mason wrote:
> > ./testopts.sh  --sc1 5.5 --sc2 5.5 --sc3 8.0 --sc 1.5
>
> zparseopts allows values to appear immediately after the option. So
> --sc3 is taken as being the same as --sc 3
> So it thinks you have four --sc options and only the last is kept.

I'll note that this (which was commented in the script):

zparseopts -D -E -A elkfloats - -sc: -sc1: -sc2: -sc3:

does the expected thing, so I suspect your awk expression to generate
the ${floats} array simply needs to sort its output differently so
that "-sc:" is appears earlier in the array than "-sc1:" etc.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-28  0:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-02 18:26 zparesopts odd behaviour Roger Mason
2021-10-27 21:22 ` Oliver Kiddle
2021-10-28  0:05   ` Bart Schaefer

Code repositories for project(s) associated with this 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).