Hi guys, I am trying to write a simple command line parser.
My question is what is the better way to handle optional command.
for example
like shell command in linux
(1)ls -a -l
(2)ls -l
(3)ls
(4)ls -a
(5)ls -a -l -s
are all valid command
When I confront this, I always stuck into multiple if ,else if ,else if conditions
And each block have very similar codes.Code:if() { ... } else if (){ ... } else if (){ ... } else if (){ }
And so many selections code just because of the order of command can exchange.
Given a specific eg.
assume
print a b c d is a valid command.
and order doesn't matter.
a b c d is optional
what I will write will become
Code:if (a) { if(b) { if (c) { if(d) // print a b c d else // print a b d } else{ if (d) // print a b d else // print a b } } else { if (c) ..... } } else { }
A really stupid solution... because if there is n optional command.
I have to at most handle 2^n selections code.
Could anyone suggest a better idea?



LinkBack URL
About LinkBacks


