Clearing the control flow when working with command line arguments [C #]

I am dealing with a program that does a lot of if ... else branches based on command line arguments. This is in C #, but I'm sure it applies to Java, C ++, etc. Here is a general outline:

if (args.Length == 0) { //do something } if (args.Length > 0 && args.Length < 2) { Console.WriteLine("Only one argument specified. Need two arguments"); return 0; } else if (args.Length > 0 && args.Length >= 2) { //Process file - Argument 1 if(args[0].Trim() == PROCESS_OPTION_ONE || args[0].Trim() == PROCESS_OPTION_TWO) { //Process file - Argument 2 if(args[1].Trim() == PROCESS_CUSTOMER || args[1].Trim() == PROCESS_ADMIN || args[1].Trim() == PROCESS_MEMBER || args[1].Trim() == PROCESS_GUEST || args[1].Trim() == PROCESS_USER ) { 

So, as you can tell, this is confusion. Is there a design pattern or two that are most applicable for cleaning things? Perhaps a command template? Thanks for the tips and advice.

+4
source share
4 answers

I partially relate to switch statements in an array of arguments and setting properties in some configuration class for each expected argument. It looks like you are expecting a string of strictly formatted argument, and not allowing the given values, you can try:

 if(args[0].Trim() == PROCESS_OPTION_ONE || args[0].Trim() == PROCESS_OPTION_TWO) { //Process file - Argument 2 switch(args[1].Trim() { case PROCESS_CUSTOMER, PROCESS_ADMIN, PROCESS_MEMBER, PROCESS_GUEST, PROCESS_USER: // Do stuff break; default: // Do other stuff break; } } 

My preferred method will look like

 foreach(string arg in args) { switch(arg) { case PROCESS_CUSTOMER: // Set property break; ... default: // Exception? break; } } 

NOTE: args.Length == 1 is faster than args.Length> 0 && args.Length <2. It is also a bit readable.

+3
source

Stop nesting.

You can switch as (+1), Joel said, or you can just break your logic into clear method calls.

 if(args.Length <= 1) { Console.WriteLine("Need 2 args kthx"); return; } if(args.Length > 2) { Console.WriteLine("More than 2 args don't know what do"); return; } var arg1 = args[0].Trim(); var arg2 = args[1].Trim(); switch(arg1) { case PROCESS_OPTION_ONE: ProcessOptionOne(arg2); break; case PROCESS_OPTION_TWO: ProcessOptionTwo(arg2); break; default: Console.WriteLine("First arg unknown I give up"); return; } 

then in your process methods ...

 private static void ProcessOptionTwo(string argumentTwo) { if(argumentTwo == PROCESS_CUSTOMER || argumentTwo == PROCESS_ADMIN || /* etc blah blah */ } 

Keep your methods as simple as possible and break them down for longer, entangling the algorithms in different method calls, which in their name give a clear idea of ​​what they are doing.

+4
source

You do not need else if you are already back. This can cut a lot of your nesting. You can also try using a switch instead of a bunch of nested ifs.

+1
source

I used the code from this code project article for a long time and made my own version for use in command line applications. I made my own modifications, for example, inherited a class from a dictionary, etc. But the regular part of the code is very good and makes these types of command line easier than a pie.

+1
source

Source: https://habr.com/ru/post/1301596/


All Articles