I have an environment with portions of the content that being conditionally typeset based on options passed to the environment. The content of this environment is predefined elsewhere so depending on the options different output is produced. This portion of the code works fine.
Now I am trying to report an error at the end of the environment if an unrecognized option was specified to detect any typos that might go unnoticed, and have run into what seems like an expansion issue. I tried all the possible uses of \edef and \expandafters that remotely made sense to me but have not found the right combination.
I do this be storing a list of all the options that were specified, and at the end I check that control sequence \<optionName>Encountered was defined for each of the specified options. In the body of the environment, I define:
\global\expandafter\def\csname <optionName> Encountered\endcsname{}%
for every possible <optionName> that could have been specified, and check against the list of options that were specified.
Note:
- There are other options in the list of options to which this sort of checking is not to be applied, so I do need to individually add them to the list as opposed to just storing
#1and doing the\foreachloop at the end over that value. - I would prefer that the options themselves be stored in the list (not their corresponding csname), as then I can better report the error message at the end of the environment.
- Also, the options may also be defined in a macro.
References:
Expected Output:
When the code in \ReportAnyUnusedOptions is uncommented and things are working, this should produce:

Code:
The problem code in \ReportAnyUnusedOptions has been commented to allow this to compile.
\documentclass{article}
\usepackage{pgffor}
\def\ListOfOptions{}
\makeatletter
\newcommand{\AddToListOfOptions}[1]{%
\g@addto@macro\ListOfOptions{#1}%
\typeout{***** Added option to check: #1}%
}
\makeatother
\newcommand{\ReportAnyUnusedOptions}{%
Checking options: \par%
\foreach \y in \ListOfOptions {%
% \y%
% \ifcsname\y Encountered\endcsname%
% ~Encountered!\par%
% \else%
% ~Not Encountered!\par%
% \fi%
}%
}%
\newenvironment{ApplyOptions}[1]{%
% Add to list of options to later check that they were used
\foreach \x in {#1} {%
% There are cases here where an option would not be
% check later, so some will be skipped
\edef\Option{\x}%
\AddToListOfOptions{\Option}%
}%
}{%
\ReportAnyUnusedOptions%
}
\begin{document}
\begin{ApplyOptions}{Option A, Option C}%
% Option A:
% if "Option A" was specified, it would be typset here
\global\expandafter\def\csname Option A Encountered\endcsname{}%
%
% Option B:
% if "Option B" was specified, it would be typset here
\global\expandafter\def\csname Option B Encountered\endcsname{}%
\end{ApplyOptions}
% Also possible to specify the options via a macro
\newcommand*{\MyOptions}{Option AA, Option CC}%
\begin{ApplyOptions}{\MyOptions}%
% Option A:
% if "Option AA" was specified, it would be typset here
\global\expandafter\def\csname Option AA Encountered\endcsname{}%
%
% Option B:
% if "Option BB" was specified, it would be typset here
\global\expandafter\def\csname Option BB Encountered\endcsname{}%
\end{ApplyOptions}
\end{document}

*keyval*packages solve? – yo' May 24 '12 at 08:20keyvalpackages... – yo' May 24 '12 at 18:58