CommandLine.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Collections.Specialized;
  6. namespace LYFZ.WinAPI
  7. {
  8. /// <summary>
  9. /// 命令行参数处理类
  10. /// </summary>
  11. public sealed class CommandLine
  12. {
  13. private string[] _arguments;
  14. private IDictionary _options;
  15. private bool _showHelp;
  16. public string[] Arguments
  17. {
  18. get
  19. {
  20. return this._arguments;
  21. }
  22. }
  23. public IDictionary Options
  24. {
  25. get
  26. {
  27. if (this._options == null)
  28. {
  29. this._options = new HybridDictionary(true);
  30. }
  31. return this._options;
  32. }
  33. }
  34. public bool ShowHelp
  35. {
  36. get
  37. {
  38. return this._showHelp;
  39. }
  40. }
  41. public CommandLine(string[] args)
  42. {
  43. ArrayList arrayList = new ArrayList();
  44. for (int i = 0; i < args.Length; i++)
  45. {
  46. char c = args[i][0];
  47. if (c != '/' && c != '-')
  48. {
  49. arrayList.Add(args[i]);
  50. }
  51. else
  52. {
  53. int num = args[i].IndexOf(':');
  54. if (num == -1)
  55. {
  56. string text = args[i].Substring(1);
  57. if (string.Compare(text, "help", StringComparison.OrdinalIgnoreCase) == 0 || text.Equals("?"))
  58. {
  59. this._showHelp = true;
  60. }
  61. else
  62. {
  63. this.Options[text] = string.Empty;
  64. }
  65. }
  66. else
  67. {
  68. this.Options[args[i].Substring(1, num - 1)] = args[i].Substring(num + 1);
  69. }
  70. }
  71. }
  72. this._arguments = (string[])arrayList.ToArray(typeof(string));
  73. }
  74. }
  75. }