I sometimes define an argument processor to (imho) simplify some code if I want to write a wrapper around another macro. For example if I write a wrapper around a macro that takes an optional *
I could end up using a number of branching tests in the form of \IfBooleanTF{#1}{...}{...}
, but I think it is convenient to do something like the following sometimes:
\newcommand\sProc[1]
{%
\IfBooleanTF{#1}
{\def\ProcessedArgument{*}}
{\def\ProcessedArgument{}}%
}
\NewDocumentCommand \wrapchap { >{\sProc}s m }
{%
\chapter#1{#2}%
}
For s
-type arguments this works quite well. However for o
-type arguments this doesn't work as shown by the following MWE
\documentclass[]{article}
\usepackage{xparse}
\newcommand\oProc[1]
{%
\IfValueTF{#1}
{\def\ProcessedArgument{[#1]}}
{\def\ProcessedArgument{}}%
}
\NewDocumentCommand \foo { >{\oProc}o m }
{%
\bar#1{#2}%
}
\renewcommand\bar[2][default]
{%
\begin{tabular}{ll}
optional & #1 \\
mandatory & #2 \\
\end{tabular}\\
}
\NewDocumentCommand \FOO { o m }
{%
\IfValueTF{#1}
{\bar[#1]{#2}}
{\bar{#2}}%
}
\begin{document}
\section{Result}
\foo{baz}
\foo[baz]{bang}
\section{Expectation}
\FOO{baz}
\FOO[baz]{bang}
\end{document}
It works with O
-type arguments, which I use in cases like this (a recent example would be this answer).
Questions:
- How can I make this work, if possible?
- Why the decision to not apply processors to
-NoValue-
by default?
EDIT: As pointed out by @egreg this is documented behaviour of xparse
. I'm well aware of this, but wonder why this design choice was made. As a result the second of the above questions is the one more interesting to me. I don't really know if the answer to the first question would break things (as I don't know if there is any current code relying on that behaviour).