Question
Is it possible to get and store a count of the optional arguments actually passed to an xparse declare argument/environment?
Purpose
Regardless of the number of arguments provided (arbitrary depending on situation), I want to do something very specific with the last one given. Because this is a variable number, I need to retrieve the count of arguments given and use that combined with # to do something with that variable.
Code
\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse}
\DeclareDocumentEnvironment{myenv}{O{}O{}O{}O{}O{}O{}O{}O{}O{}O{}} % 10 optional args
{startcode}
{endcode}
\DeclareDocumentCommand{\mycom}{O{}O{}O{}O{}O{}O{}O{}O{}O{}O{}}{% 10 optional args
}%
\begin{document}
\mycom[a][b][c] % Count 3
% Do something special with #3
\begin{myenv}[a][b][c][d] % Count 4
% Do something special with #4
\end{myenv}
\end{document}

\IfValueTF{#9}{9 arguments}{\IfValueTF{#8}{8 arguments}{..}}would work. By the way, why not declare the last one asmmandatory, and the eight before aso?\foo[a][b][c][d]{e}. – Manuel May 06 '15 at 09:51\IfValueTFbut I was hoping for a "cleaner" solution (less code). I think just making the ninth argument mandatory is a good idea and should work for my purposes! I did not know about the maximum number of arguments. – Jonathan Komar May 06 '15 at 10:45mis that I still do not know whetherm=#1,#2,#3,#4,#5,#6,#7,#8,or#9. – Jonathan Komar May 06 '15 at 10:52\foo[a,b,c,d,e,f]expl3 then makes it easy to count the list, iterate over it, select the last etc. – David Carlisle May 06 '15 at 12:04xparse(or even the traditional way) can't know which of 1,2,3,4,5,6,7,8,9 optional arguments is really the one under supervision? Does\foo[This][is]meanThisis the 1st or may be the 5th opt. argument? – May 06 '15 at 12:04[][][]is the wrong input syntax for that (irrespective of coding and efficiency issues) – David Carlisle May 06 '15 at 12:30xparseis meant for defining LaTeX2e-like interface: it's not a general 'parse some input' set up. I wonder if you might be better with a delimited argument, so\foo a b c d e\stopor similar (where\stopmay be defined naturally by your real use case). – Joseph Wright May 06 '15 at 12:34{oooooooom}you ALWAYS have#9as the mandatory argument. If you use\foo[a]{b}then#1isaand#9isb(the rest are “empty”). If you have\foo[a][b][c][d]{e}then#1–#4area–dand#9ise(the rest are “empty”). I don't see what's the problem. In any case, I agree, this is not the optimal solution (more than 2 or 4 arguments is really a bad sign for a “user macro”). – Manuel May 06 '15 at 13:01