一个简单的命令行解析工具类

作者:追风剑情 发布于:2018-12-1 18:39 分类:C#

示例代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApp4
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string line = "cmd -quit -output \"C:\\Program Files (x86)\" -update:mode -2 -5 -save \"C:\\Program Files (x86)\"";
  13. CommandLine cmd = new CommandLine();
  14. cmd.Parse(line);
  15. cmd.Print();
  16.  
  17. Console.WriteLine("是否存在 {0}: {1}", "-quit", cmd.ExistParam("-quit"));
  18. Console.WriteLine("是否存在 {0}: {1}", "-output", cmd.ExistParam("-output"));
  19. Console.WriteLine("参数值为 {0}: {1}", "-output", cmd.GetParamValue("-output"));
  20. Console.Read();
  21. }
  22. }
  23. }
  24.  
  25. public class CommandLine
  26. {
  27. private char[] _chars;
  28. private int _pos = 0;
  29. private string _whiteSpace = " \t\r\n";
  30. private List<string> _params = new List<string>();
  31.  
  32. public CommandLine()
  33. {
  34. }
  35.  
  36. public void Parse(string line)
  37. {
  38. this._chars = line.ToCharArray();
  39.  
  40. _params.Clear();
  41. string p = NextParam();
  42. while (!string.IsNullOrEmpty(p)) {
  43. _params.Add(p);
  44. p = NextParam();
  45. }
  46. }
  47.  
  48. public string[] GetParams()
  49. {
  50. return _params.ToArray();
  51. }
  52.  
  53. //判断参数是否存在
  54. public bool ExistParam(string p)
  55. {
  56. return _params.Contains(p);
  57. }
  58.  
  59. //获取参数值
  60. public string GetParamValue(string p)
  61. {
  62. for (int i=0; i< _params.Count; i++) {
  63. if (_params[i] == p) {
  64. if (i+1 < _params.Count) {
  65. return _params[i+1];
  66. }
  67. break;
  68. }
  69. }
  70. return null;
  71. }
  72.  
  73. private string NextParam()
  74. {
  75. EatWhiteSpace();
  76. int start_pos = _pos;
  77. char c = NextChar();
  78. //如果参数是用双引号括起来的
  79. bool is_double_quote = IsDoubleQuote(c);
  80. if (is_double_quote) {
  81. //找到关闭引号"所在位置
  82. while (!IsDoubleQuote(NextChar()));
  83. }else{
  84. while (!IsWhiteSpace(NextChar()));
  85. }
  86.  
  87. int length = 0;
  88. if (is_double_quote){
  89. //去掉参数两边的双引号
  90. start_pos++;
  91. }
  92. length = _pos - start_pos - 1;
  93. if (length <= 0)
  94. return null;
  95.  
  96. string s = new string(_chars, start_pos, length);
  97. //s = s.Trim();
  98. return s;
  99. }
  100.  
  101. private char NextChar()
  102. {
  103. if (_pos >= _chars.Length)
  104. return '\n';
  105. char c = _chars[_pos];
  106. _pos++;
  107. return c;
  108. }
  109.  
  110. private bool IsDoubleQuote(char c)
  111. {
  112. return c == '"';
  113. }
  114.  
  115. private bool IsWhiteSpace(char c)
  116. {
  117. return _whiteSpace.IndexOf(c) != -1;
  118. }
  119.  
  120. private void EatWhiteSpace()
  121. {
  122. if (_pos >= _chars.Length)
  123. return;
  124. while (IsWhiteSpace(_chars[_pos] )) {
  125. _pos++;
  126. if (_pos >= _chars.Length)
  127. break;
  128. }
  129. }
  130.  
  131. public void Print()
  132. {
  133. for(int i=0; i< _params.Count; i++)
  134. {
  135. Console.WriteLine(_params[i]);
  136. }
  137. }
  138. }

运行测试

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号