C# - Command line Tools n Modularizing IF-ELSE

I have been writing some of the command line tools which handles lots of functionality. I used to end up writing lots of IF-ELSE which are non reusable code. What I think we should do to make such tools really extensible is defining the commands within an XML file.

<command name="displayallnames" class="[class and assembly name]" />

Once a person enters a particular option we traverse through the input XML file to pick the required class and call necessary methods.
An interface ICommand like defines what we expect from such commands.

namespace code.grep.in.commandline
{
enum ExecResult
{
int SUCCESS = 0;
int FAILURE = 1;
int INVALID_USAGE = 2;
}
interface ICommand {
ExecResult execute(Arguments args);
void usage();
}
}

Now what we can do is in the command line tools is to read the XML file and using reflection call the corresponding classes execute method.

I will post the complete implementation soon!

Labels: ,


About this entry