Write a small tool for parsing C # command line parameters,
Recently, many tests have been conducted. Therefore, you often need to create some console-type applications. Because the program has different parameter switches, you need to pass various switches and parameters to the program through the command line when the program starts. It is inconvenient to directly operate on args, so I wrote a small tool for parsing parameters to process various parameters.
Parameter entity:
1 public class CommandLineArgument 2 { 3 List<CommandLineArgument> _arguments; 4 5 int _index; 6 7 string _argumentText; 8 9 public CommandLineArgument Next10 {11 get {12 if (_index < _arguments.Count - 1) {13 return _arguments[_index + 1];14 }15 16 return null;17 }18 }19 public CommandLineArgument Previous20 {21 get {22 if (_index > 0)23 {24 return _arguments[_index - 1];25 }26 27 return null;28 }29 }30 internal CommandLineArgument(List<CommandLineArgument> args, int index, string argument)31 {32 _arguments = args;33 _index = index;34 _argumentText = argument;35 }36 37 public CommandLineArgument Take() {38 return Next;39 }40 41 public IEnumerable<CommandLineArgument> Take(int count)42 {43 var list = new List<CommandLineArgument>();44 var parent = this;45 for (int i = 0; i < count; i++)46 {47 var next = parent.Next;48 if (next == null)49 break;50 51 list.Add(next);52 53 parent = next;54 }55 56 return list;57 }58 59 public static implicit operator string(CommandLineArgument argument)60 {61 return argument._argumentText;62 }63 64 public override string ToString()65 {66 return _argumentText;67 }68 }
Parameter Parser:
1 public class CommandLineArgumentParser 2 { 3 4 List<CommandLineArgument> _arguments; 5 public static CommandLineArgumentParser Parse(string[] args) { 6 return new CommandLineArgumentParser(args); 7 } 8 9 public CommandLineArgumentParser(string[] args)10 {11 _arguments = new List<CommandLineArgument>();12 13 for (int i = 0; i < args.Length; i++)14 {15 _arguments.Add(new CommandLineArgument(_arguments,i,args[i]));16 }17 18 }19 20 public CommandLineArgument Get(string argumentName)21 {22 return _arguments.FirstOrDefault(p => p == argumentName);23 }24 25 public bool Has(string argumentName) {26 return _arguments.Count(p=>p==argumentName)>0;27 }28 }
By introducing these two classes in the project, you can parse and operate args in the Main function.
For Example, if the console application Example is available, enter the following in the command line:
Example.exe -u MrJson -p admin123
Process args in the Main function of Example:
1 static void Main (string [] args) 2 {3 var arguments = CommandLineArgumentParser. parse (args); 4 5 if (arguments. has ("-u") 6 {7 Console. writeLine ("username: {0}", arguments. get ("-u "). next); 8} 9 10 if (arguments. has ("-p") 11 {12 Console. writeLine ("Password: {0}", arguments. get ("-p "). next); 13} 14}
If multiple values need to be passed after the parameter, for example, in the following example, the-chpwd parameter requires two parameters:
Example.exe -chpwd admin888 admin999
Then, you can handle the problem as follows:
1 if (arguments. has ("-chpwd") 2 {3 var arg = arguments. get ("-chpwd"); 4 var oldPwd = arg. take (); 5 var newPwd = arg. take (). take (); 6 // or 7 var pwds = arg. take (2); 8 oldPwd = pwds. first (); 9 newPwd = pwds. last (); 10 11 Console. writeLine ("original password: {0} new password: {1}", oldPwd, newPwd); 12}
That's all.