Search code examples
zsh

Have I found a bug in zparseopts or am I doing this wrong


I ended up on a mission to document/solve zparseopts this afternoon. I think I found a bug with the -K option - or as I call it -KillYourself.

I think -K is not handling a flag option correctly. The output skews previously existing key/value pairs one-half pair to the left as shown below.

Can a BASH / ZSH guru make sure that I am doing this correctly?

Expected Behavior with -K and additional options and flags:

Dictionary:

--key           -> greg
--flag          -> 
default         -> Mr. Yutz Was Here

Expected Behavior with -K and no other options:

Dictionary:

default         -> Mr. Yutz Was Here

Curious Possibly Broken Behavior:

Dictionary:

--key                   -> greg
--flag                  -> default
Mr. Yutz Was Here       -> 

It looks like the Associative Array gets "bumped" a half value to the left.

#!/usr/bin/env zsh


declare -a pargs        #numveric index array
declare -A paargs       #associative array - or dictionary

pargs=("Mr. Yutz Was Here")
paargs[default]="Mr. Yutz Was Here"

echo "\n=====\npargs currenty:\n$pargs"
echo "\n=====\npaargs currenty:\n$paargs"

# -K - KILL YOURSELF - and KEEP previous settings like defaults you pumped into the array before processing.  As long as they are not set below.  Then this is meaningless.
#      Seriously....if you use this you will want to kill yourself.  If you have a flag, the Associative array breaks.  Ugh.
#      to recreate bug, run without arguments.  Then run again with --flag --key FooBar.  Delete -K and do it again.
#      The "Dictionary Output will be borked."
# -D - DELETE - pops each item from the input and processes the next element as "the first one" of $1 if you want to be all shelly about it
# -E - NO ERROR - don't stop on an error.  keep going until done or -- is hit
# -a - ARRAY for the results indexed from 1
# -A - DICTIONARY for the results in key / value pairs.  Flags don't have values.  Just "presence"

zparseopts -D -E -a pargs -A paargs -flag -key:

printf "\n=====\nArray of Results:\n\n"

for ((i = 1; i <= $#pargs; i++)) 
do
    echo "item: $i \t-> $pargs[$i]"
done

echo "\n====="

printf "Dictionary:\n\n"
for key value in ${(kv)paargs}
do
    echo "$key \t-> $value"
done
echo "=====\n"

# Flag detection.
printf "flag=%s key=%s\n\n" ${pargs[(I)--flag]} ${paargs[--key]}
printf "%s\n\n" "$*"

This behaves as I would expect: Output without -K:

greg@api:~/projects/test_swarm$ ./zsh_test.sh --flag --key greg

=====
pargs currenty:
1: Mr. Yutz Was Here

=====
paargs currenty:
default: Mr. Yutz Was Here

=====
Array of Results:

item: 1         -> --flag
item: 2         -> --key
item: 3         -> greg

=====
Dictionary:

--key   -> greg
--flag  -> 
=====

flag=1 key=greg

Output with -K:

greg@api:~/projects/test_swarm$ ./zsh_test.sh --flag --key greg

=====
pargs currenty:
1: Mr. Yutz Was Here

=====
paargs currenty:
default: Mr. Yutz Was Here

=====
Array of Results:

item: 1         -> --flag
item: 2         -> --key
item: 3         -> greg

=====
Dictionary:

--key   -> greg
--flag  -> default
Mr. Yutz Was Here       -> 
=====

flag=1 key=greg

Solution

  • The issue is the printout of the values in the associative array. Try replacing the paargs for loop with this:

    printf 'paargs %s: %s\n' "${(kv@)paargs}"
    

    The significant changes are the addition of double quotes and @ parameter expansion flag. zsh is usually very forgiving with quoting, but there are some cases where it makes a difference.

    It's often easier to display variable values with the typeset builtin:

    typeset -p pargs paargs
    

    (meta note: do we need a zparseopts tag?)