Q:

Different command definitions with more than one optional arguments arguments

I was reading the following post Different command definitions with and without optional argument on how to implement Different command definitions with and without optional argument. The example mentioned in this link covers the case of one optional argument. Following the same guidelines, I have been trying to do the same with two optional arguments using the following code without any luck:
\makeatletter
\@ifundefined{myDummy}
{

\def\myDummy
{
    \@ifnextchar[
    {\@WithAtLeastParam}{\@WithoutParam}
}
\def\@WithoutParam#1{x_{#1}}
\def\@WithAtLeastParam[#1]
{
    \@ifnextchar[
    {\@WithTwoParam}{\@WithOneParam}
}
\def\@WithTwoParam#1[#2]#3{x_{#1\rightarrow #2,#3}}

\def\@WithOneParam#1#2{x_{#1\rightarrow #2}}
}
\makeatother

Could any one please tell me what I have been doing wrong?

A:

You need to comment out the newline after \def\@WithAtLeastParam[#1], else the argument must be followed by a space and TeX will fail to parse the macro arguments if there is no space. This is not necessary for \def\myDummy because of TeX's rules for spaces following control sequences, but behind each \def which's line ends with something that is not a control sequence the line ending should be commented. I also commented each parasitic space which could sneak into the macro (though those won't matter as the macro is used in math mode).
Also \@WithAtLeastParam grabs the first parameter, which is why you have to forward the grabbed argument to both \@WithTwoParam and \@WithOneParam.
I removed the \@ifundefined test for simplicity, you might add it again.
\documentclass[]{article}

\makeatletter
\def\myDummy%
  {%
    \@ifnextchar[
      {\@WithAtLeastParam}
      {\@WithoutParam}%
  }
\def\@WithoutParam#1{x_{#1}}
\def\@WithAtLeastParam[#1]%
  {%
    \@ifnextchar[
      {\@WithTwoParam{#1}}
      {\@WithOneParam{#1}}%
  }
\def\@WithTwoParam#1[#2]#3%
  {%
    x_{#1\rightarrow #2,#3}%
  }
\def\@WithOneParam#1#2%
  {%
    x_{#1\rightarrow #2}%
  }
\makeatother

\begin{document}
$\myDummy{1} \myDummy[1]{2} \myDummy[1][2]{3}$
\end{document}

The same behaviour but implemented using xparse:
\documentclass[]{article}

\usepackage{xparse}
\NewDocumentCommand \myDummy { o o m }
  {%
    x_{\IfValueT{#1}{#1\rightarrow}\IfValueT{#2}{#2,}#3}%
  }

\begin{document}
$\myDummy{1} \myDummy[1]{2} \myDummy[1][2]{3}$
\end{document}

