This question piggy-backs on the answer to Capture a macro's subscripts/superscripts by the macro by egreg.
I'm attempting to use the xparse package to create some macros and I'd rather not have to use different macros when I want to have sub and superscripts (or both at once!).
I'd like a macro like this:
\F
\F{m}
\F{m}_b
\F{m}^a
\F{m}_b^a
\F{m}^a_b
to produce output equivalent to this:

$F$
$F[m]$
$F_b[m]$
$F^a[m]$
$F_b^a[m]$
$F^a_b[m]$
Based on the abolve linked question, I can capture one or the other, but not both. I've made a few attempts, but there must be something very fundamental (or subtle) about now @ifnextchar works that is beyond me.
This code takes two optional arguments, rather than one, but the issue remains: The sub and superscripts always end up at the end of the line, not on the defined first character.
\documentclass{article}
\usepackage{xparse}
\makeatletter
\newcommand{\DFcmd}{\ensuremath{F}}
\NewDocumentCommand\DFhelp{ g g g g }
{
\IfValueT{#3}{ \def\@DFcmdsub{\DFcmd^{#3}} }{ \def\@DFcmdsub{\DFcmd} }
\IfValueT{#4}{ \def\@DFcmdsuper{\@DFcmdsub_{#4}} }{ \def\@DFcmdsuper{\@DFcmdsub} }
\@DFcmdsuper
\IfNoValueF{#1}{#1}{}
\IfNoValueF{#2}{#2}{}
}
\NewDocumentCommand\DF{ o o }
{%
\def\firstarg{}%
\def\secondarg{}%
\IfNoValueF{#1}{ \def\firstarg{[#1]} }{ \def\firstarg{} }%
\IfNoValueF{#2}{ \def\secondarg{#2} }{ \def\secondarg{} }%
\def\superopt{}%
\def\subopt{}%
\@ifnextchar^%
{%
\assign@superopt%
\@ifnextchar_%
{%
\assign@subopt%
}%
{%
\def\subopt{}%
}%
}%
{%
\@ifnextchar_%
{%
\assign@subopt%
\@ifnextchar^%
{%
\assign@superopt%
}%
{%
\def\superopt{}%
}%
}
{%
\def\subopt{}%
}%
}
\DFhelp{\firstarg}{\secondarg}{\superopt}{\subopt}%
}
\def\assign@superopt^#1{ \def\superopt{#1} }
\def\assign@subopt_#1{ \def\subopt{#1} }
\makeatother




\If*ValueTor\If*ValueFbut use both branches. Then you specify two optional arguments for\DFwhich are enclosed in[and], but you never use them.\@ifnextcharfinds only\bgroupfrom the{m}argument. Furthermore, you use\assign@superoptright before\@ifnextcharalthough it expects a^, this won’t work! Use the next\@ifnextcharinside the definition of\assign@*opt(after it has grabbed its argument). – Qrrbrbirlbel Jun 10 '13 at 19:42$F_a^b[m]$. Or, if you don't want to have an explicit "F",$\DF_a^b[m]$, after defining\DFto do what you desire. – egreg Jun 10 '13 at 19:56