\label
needs the reference info in \@currentlabel
, but \addcontentsline
does nothing to \@currentlabel
. You could do for instance:
\makeatletter
\def\@currentlabel{\p@section\thesection}%
\makeatother
or more explicitly, \makeatletter\def\@currentlabel{0}\makeatother
(or another value) right before using \label
to define this info, but the usual way to set \@currentlabel
is to use \refstepcounter
.
Since you appear to want 0 as the section number and \refstepcounter
increments the counter before storing the \protected@edef
-expansion of \p@<counter>\the<counter>
in \@currentlabel
, I use \setcounter{section}{-1}
before the first \refstepcounter{section}
call here.
\documentclass{l3doc}
\usepackage{hyperref}
\makeatletter
\newcommand*{\mySetNameref}{\def\@currentlabelname}
\makeatother
\begin{document}
\tableofcontents
\bigskip
% First option
\leavevmode
% The next “section” will be numbered 0
\setcounter{section}{-1}\refstepcounter{section}%
\mySetNameref{Option \texttt{Foo}}%
\label{opt:foo}%
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Foo}%
\DescribeOption{Foo} Description of \texttt{Foo}
% Second option
\leavevmode
\refstepcounter{section}%
\mySetNameref{Option \texttt{Bar}}%
\label{opt:bar}%
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Bar}%
\DescribeOption{Bar} Description of \texttt{Bar}
\medskip
Section~\ref{opt:foo} is named \nameref{opt:foo}. Section~\ref{opt:bar} is
named \nameref{opt:bar}.
\end{document}

Of course, you should probably wrap this in a macro to make it more automatic.
\DescribeOption{Foo}
using\documentclass{article}
, then probably more straightforward to stick tol3doc
. I'm open to a contrary opinion.\changes
. I have just realized that it works after exec the glo+index commands given in the*log
file' (Package l3doc Info: Generate the change,,,).