I made a macro using xparse for formatting the month with optional day and year. Except the appended optional argument doesn't work inside a description label for the description list---unless I enclose it in curly braces (e.g. {\Month{4}[8]}). This error is easily fixable via curly braces, but I would like to know WHY it occurs and perhaps also how to avoid having to fix it with curly braces.
\documentclass{article}
\usepackage{stix2}
\usepackage{etoolbox,xparse,xspace}
%\numtomonth converts a number into its corresponding month.
%The starred and unstarred versions display the long and short forms of the month.
\makeatletter
\NewDocumentCommand{\numtomonth}{ s m }{%
\IfBooleanTF{#1}%
{%ifstar
\ifnumequal{#2}{1}{January}{%
\ifnumequal{#2}{2}{February}{%
\ifnumequal{#2}{3}{March}{%
\ifnumequal{#2}{4}{April}{%
\ifnumequal{#2}{5}{May}{%
\ifnumequal{#2}{6}{June}{%
\ifnumequal{#2}{7}{July}{%
\ifnumequal{#2}{8}{August}{%
\ifnumequal{#2}{9}{September}{%
\ifnumequal{#2}{10}{October}{%
\ifnumequal{#2}{11}{November}{%
\ifnumequal{#2}{12}{December}{%
\errmessage{The input must be an integer between 1 and 12}%
}}}}}%
}}}}}%
}}%
}%
{%ifnostar
%https://tex.stackexchange.com/questions/15009/macros-for-common-abbreviations
\ifnumequal{#2}{1}{Jan\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{2}{Feb\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{3}{March}{%
\ifnumequal{#2}{4}{April}{%
\ifnumequal{#2}{5}{May}{%
\ifnumequal{#2}{6}{June}{%
\ifnumequal{#2}{7}{July}{%
\ifnumequal{#2}{8}{Aug\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{9}{Sep\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{10}{Oct\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{11}{Nov\@ifnextchar{.}{}{.\@\xspace}}{%
\ifnumequal{#2}{12}{Dec\@ifnextchar{.}{}{.\@\xspace}}{%
\errmessage{The input must be an integer between 1 and 12}%
}}}}}%
}}}}}%
}}%
}%
}
\makeatother
%
%\Month has three arguments.
%The first and third are optional.
%The first is the year and the third is the day of the month.
%The mandatory argument is the number corresponding to the month.
%\Month displays the short form of the month and, if used, the day and the year.
\NewDocumentCommand{\Month}{ O{} m O{} }{%\month already defined
\numtomonth{#2}%
\ifstrempty{#3}{}{~#3}%
\ifstrempty{#1}{}{%
\ifstrempty{#3}{ #1}{, #1}%
}%
}
\usepackage{enumitem}
\parindent=0pt
\begin{document}
I am trying to make a description list on \Month[2023]{4}[8].%random sentence
\begin{description}
\item[\Month{4}]%works; no appended optional argument
Alpha
\item%[\Month{4}[8]]%doesn't work with appended optional argument
Beta
\item[{\Month{4}[8]}]%works; same as above, but enclosed in curly braces
Charlie
\item%[\Month[2023]{4}[8]]%doesn't work with appended optional argument
Delta
\item[{\Month[2023]{4}[8]}]%works; same as above, but enclosed in curly braces
Epsilon
\end{description}
\end{document}

\itemis a classic command not defined using\NewDocumentCommand. So automatic nesting of commands with optional argument(s) inside the optional argument of\itemis not provided. – cabohah Apr 08 '23 at 08:51