Best way to compare string arrays (command line commands)

In my application, I create a custom command line. I am looking for a better way to check the user-entered commands and the argument again the arguments that I defined. Take the following example command (which works if the user enters it correctly and in order)

do>Drawer /X:Time /Y:Current /N:Window NAme /D:Description 

Now I want to have a method for checking for me:

 private string CheckDrawerArgs(string[] args) { var mustExist = new string[4]{"X:", "Y:", "N:", "D:"}; if(args.Length != mustExist.Length) { return "Arguments are not completly defined. use 'Drawer /?' for help."; } var argsAreRight = false; var flat = from s1 in args from s2 in mustExist where s1.StartsWith(s2) //how to check if all elements provided // in args does look // like (Starts with) elements in mustExist ; if(argsAreRight == false) { return "Bad arguments"; } //Proceed with rest... } 

So, what I'm looking for is to check if the necessary arguments provided by the user are needed in args , and they are not duplicated, and their order will not affect the check ...

Waiting for clues!

+4
source share
3 answers

In addition to Henk's answers after testing and working

  var allpresent = args.Length == mustExist.Length && args.All(c => mustExist.Any(e => c.ToString().StartsWith(e))); 
+1
source

I would recommend writing it in a more reusable way. The fact that all parameters must be set must not be hardcoded into the logic of the parameter parser. You should just check it out at the end, just before you โ€œcontinue your restโ€.

In such cases, I usually do the following:

First of all, for each of the possible arguments, I have some variables that contain reasonable default values โ€‹โ€‹(so that the argument can be omitted), a trivial example of which would be bool argument_x_given = false;

So, I loop around the given arguments, and inside the loop I check the current argument against each of the possible arguments to find what it is. If not found, we have an error. If a suitable argument is found, I analyze the rest of the argument (the material after the ":"), and I set the variables associated with the argument. By doing this, I verify that the argument does not duplicate. In this trivial example, it will be if( argument_x_given ) { --error-- } else { argument_x_given = true; ... } if( argument_x_given ) { --error-- } else { argument_x_given = true; ... } .

Finally, as soon as the loop is finished, I make sure that all the necessary arguments have been specified.

So, Iโ€™m trying to say that you wonโ€™t gain anything by comparing arrays of strings, because you still have to understand each of your arguments, and also comparing string arrays is like trying to take advantage of a situation that is very specific to this problem and cannot reused.

+3
source

None of the above examples takes into account the slash characters that begin with each command line argument. You may need to modify your mustExist to include a slash:

 var mustExist = new string[4]{"/X:", "/Y:", "/N:", "/R:"}; 

Another option is to disconnect them from the arguments before comparing.

+1
source

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


All Articles