CommandLine类用来解析命令行字符串,通过扩展子类来实现不同命令的功能。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace CommandLineTool { class Program { static void Main(string[] args) { //测试命令 CommandLine cmdLine = new CommandLine("jar -cvf xxx.jar *"); CommandLine cmdLine1 = new CommandLine("copy \"C:\\Program Files\\text.txt\" \"D:\\dir1\\text.txt\""); Console.WriteLine(cmdLine.ToString()); Console.WriteLine(cmdLine1.ToString()); CommandLine cmdLine2 = CommandLine.Create("copy \"C:\\Program Files\\text.txt\" \"D:\\dir1\\text.txt\""); Console.WriteLine(cmdLine2.ToString()); if (null != cmdLine2) cmdLine2.Execute(); Console.Read(); } } public class CommandLine { private string _origin; private string _cmd; private string[] _params; public string cmd { get { return _cmd; } } public string[] cparams { get { return _params; } } public static CommandLine Create(string origin) { string cmd = ParseCmdType(origin); char[] cmdChars = cmd.ToCharArray(); //首字母转成大写 if (cmdChars[0] >= 97) cmdChars[0] = (char)((byte)cmdChars[0] - 32); string className = new string(cmdChars) + "Command"; Type type = Type.GetType("CommandLineTool."+className); CommandLine cmdLine = null; if (null != type) { cmdLine = (CommandLine)Activator.CreateInstance(type); cmdLine.SetOrigin(origin); } return cmdLine; } private static string ParseCmdType(string s) { string cmd; int index = s.IndexOf(" "); if (-1 == index) cmd = s; else cmd = s.Substring(0, index); return cmd; } public CommandLine() { } public CommandLine(string origin) { SetOrigin(origin); } public void SetOrigin(string origin) { if (string.IsNullOrWhiteSpace(origin)) throw new Exception("命令行不能为空"); _origin = origin.Trim(); ParseCmd(_origin); } private void ParseCmd(string s) { int index = s.IndexOf(" "); if (-1 == index) { _cmd = s; } else { _cmd = s.Substring(0, index); string p = s.Substring(index).Trim(); ParseParams(p); } } private void ParseParams(string s) { List<int> spaceIndexs = new List<int>(); for (int i = 0; i < s.Length; i++) { //发现前引号 if (s[i] == '"') { i = IndexOfquo(s, i); } else if (s[i] == ' ')//发现参数分隔符 { i = IndexOfSpace(s, i); spaceIndexs.Add(i); } } _params = new string[spaceIndexs.Count + 1]; spaceIndexs.Insert(0, 0); spaceIndexs.Add(s.Length); for (int i = 0; i+1 < spaceIndexs.Count; i++) { int start = spaceIndexs[i]; int end = spaceIndexs[i+1]; _params[i] = TrimQuo(Substring(s, start, end).Trim()); } } //搜索连续空格中的最后一个空格索引号 private int IndexOfSpace(string s, int start) { int index = start; for (index = start + 1; index < s.Length; index++) { if (s[index] != ' ') { index--; break; } } return index; } //搜索后引号 private int IndexOfquo(string s, int start) { int index; for (index = start + 1; index < s.Length; index++) { if (s[index] == '"' && s[index - 1] != '\\') { break; } } return index; } private string Substring(string s, int start, int end) { s = s.Substring(start, end - start); return s; } //去除字符串前后引号 private string TrimQuo(string s) { if (s[0] == '"' && s[s.Length - 1] == '"') { s = s.Substring(1, s.Length - 2); } return s; } public override string ToString() { StringBuilder sb = new StringBuilder(); if (null != _params) { for (int i = 0; i < _params.Length; i++) sb.AppendFormat("{0} ,", _params[i]); } string s = string.Format("cmd={0}; params={1}", cmd, sb.ToString()); return s; } /// <summary> /// 执行具体功能,此方法由子类实现。 /// </summary> public virtual void Execute() { } } //Copy命令 public class CopyCommand : CommandLine { public override void Execute() { Console.WriteLine("执行CopyCommand"); } } }
运行测试