Parsing optional macro arguments

A solution using LaTeX2e just for \@ifnextchar:

\documentclass{article}
\makeatletter
\def\display#1{%
  \@ifnextchar[%]
    {\display@aux@i{#1}}
    {\display@aux@i{#1}[]}%
}
\def\display@aux@i#1[#2]#3=#4;{%
  \display@aux@ii{#1}{#2}{#3}#4||\@nil
}
\def\display@aux@ii#1#2#3#4|#5|#6\@nil{%
  *#1*#2*#3*#4*#5*
}
\makeatother
\begin{document}
\noindent
\display{aaa}[bbb] AAA = BBB | CCC; \\
\display{aaa}[bbb] AAA = BBB ;
\end{document}

Alternative xparse-based solution:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareDocumentCommand \display 
  { m o u { = } > { \SplitArgument { 1 } { | } } u { ; } } {
  * #1 * #2 * #3 * \display_aux:nn #4 *  
}
\cs_new:Npn \display_aux:nn #1#2 { #1 * #2 }
\ExplSyntaxOff
\begin{document}
\noindent
\display{aaa}[bbb] AAA = BBB | CCC; \\
\display{aaa}[bbb] AAA = BBB ;
\end{document}

the same, only the other way round :-)

\documentclass{minimal}

\makeatletter
\def\display#1{\@ifnextchar[{\display@i{#1}}{\display@i{#1}[]}}
\def\display@i#1[#2]#3;{\display@ii#3||\@nil{#1}[#2]}
\def\display@ii#1=#2|#3|#4\@nil#5[#6]{*#5*#6*#1*#2*#3*}

\def\blub{\@ifnextchar[\blub@i{\blub@i[]}}
\def\blub@i[#1]#2;{\blub@ii[#1]#2++\@nil}
\def\blub@ii[#1]#2+#3+#4\@nil{blub[#1] & #2 & #3}
\makeatother


\makeatother

\begin{document}

\display{aaa}[bbb] AAA = BBB|CCC;

\display{aaa}[bbb] AAA = BBB;

\end{document}

You have already received valid LaTeX solution, but for someone else who wants to do something similar in ConTeXt, I am including a ConTeXt solution. This is essential similar to LaTeX solutions using \@ifnextchar, but use a higher level macro \dodoubleargument that checks for two optional arguments (ConTeXt also provides other variants of \do...argument: single, double, triple, quadruple, quintuple, sixtuple, and seventuple).

\def\Macro#1%
  {\dodoubleargument\doMacro[#1]}

\def\doMacro[#1][#2]#3=#4;%
  {\dostartMacro{#1}{#2}{#3}#4||\dostopMacro}

\def\dostartMacro#1#2#3#4|#5|#6\dostopMacro
  {*#1*#2*#3*#4*#5*}

\starttext
\startlines
\Macro{aaa} AAA = BBB ;
\Macro{aaa} AAA = BBB | CCC ;
\Macro{aaa}[bbb] AAA = BBB ;
\Macro{aaa}[bbb] AAA = BBB | CCC; 
\stoplines
\stoptext

Tags:

Macros