Why am I processing a comma-separated list incorrectly when using etoolbox and kvoptions?

I'm trying to pick something out of a list that has been passed from some package options by kvoptions, but docsvlist doesn't appear to be recognising the commas.

I suspect this may be something to do with the Knuth "mouth and stomach" paradigm, but I'm not enough of a TeXnician to understand why, nor how to do it correctly. Can someone explain what's going on?

Here's my MWE:

testx.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testx}
\RequirePackage{kvoptions}
\RequirePackage{etoolbox}
\SetupKeyvalOptions{family=test,prefix=test@}
\DeclareStringOption{refnames}
\ProcessKeyvalOptions*
\newcommand{\vtag}{(None)}
\csdef{xferret}{}      % This is what I want to extract
\newcommand{\testtag}[1]{%
\PackageWarning{testx}{Processing #1}
\def\do##1{%
\ifcsdef{x##1}{%
\renewcommand{\vtag}{##1}%
\listbreak
}{%
\PackageWarning{testx}{##1 not selected}
}
}%
\docsvlist{#1}%
}%
\testtag{\test@refnames}


test.tex:

\documentclass[10pt,a4paper]{article}
\usepackage[refnames={rat, ferret, 1.0, mole}]{testx}
\begin{document}
\vtag
\testtag{rat, ferret, 1.0, mole}
\vtag
\end{document}


The first \vtag (set in the package) produces "(None)"

The second \vtag produces "ferret"

in the log, we have this:

(from the package):

Package testx Warning: Processing rat, ferret, 1.0, mole on input line 22.
Package testx Warning: rat, ferret, 1.0, mole not selected on input line 22.


(from the call in the document body)

Package testx Warning: Processing rat, ferret, 1.0, mole on input line 5.
Package testx Warning: rat not selected on input line 5.

-

This is a classic expansion issue. What happens is that

\testtag{\test@refnames}


passes \test@refnames exactly as written. In writing to the log, this is fully-expanded by TeX, so you see what you expect. However

\docsvlist{#1}%


sees \test@refnames as #1, with no commas. So you need to expand \test@refnames correctly. Depending on other uses, either to

\expandafter\testtag\expandafter{\test@refnames}


at point of use or

\expandafter\docsvlist\expandafter{#1}%


in the internal macro (but not both!). In either case, what happens here is that TeX will follow the \expandafter to convert \test@refnames into it's content before expanding the processing macro.

-
Joseph, thank you very much. \expandafter\docsvlist\expandafter{#1} fixes it nicely. –  Brent.Longborough Aug 28 '11 at 12:18