# [Tex/LaTex] How to declare an option that takes an input value

package-writingpackages

I know I can declare and execute an option for my package using

\DeclareOption{myoption}{\typeout{I did it!}}
\ProcessOptions


but now I'd like to give the option a value:

\DeclareOption{type}{\typeout{You typed: \CurrentOptionValue}}


which would be used as follows:

\usepackage[type="hello, world!"]{mypackage}


Requirements on the value string is that it can contain at least some nonalphanumeric characters, like spaces, commas and dots (and maybe some others…).

Of these, kvoptions is probably the most robust approach. The way that options are declared is to use \define@key (or \pgfkeys in the case of pgfopts) before processing the package options. With kvoptions you get some nicer wrappers, such as \DeclareBoolOption.
\def\mysetupmacro#1{\setkeys{mypackage}{#1}}

which will then only need a basic key-value package: probably I would choose kvsetkeys, which provides \kvsetkeys as a more robust version of \setkeys.