CommandLine.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // **********************************************************************************
  2. // CassiniDev - http://cassinidev.codeplex.com
  3. //
  4. // Copyright (c) 2010 Sky Sanders. All rights reserved.
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // This source code is subject to terms and conditions of the Microsoft Public
  8. // License (Ms-PL). A copy of the license can be found in the license.txt file
  9. // included in this distribution.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // **********************************************************************************
  14. #region
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Specialized;
  18. #endregion
  19. namespace CassiniDev.UIComponents
  20. {
  21. public sealed class CommandLine
  22. {
  23. #region Fields
  24. private readonly string[] _arguments;
  25. private readonly bool _showHelp;
  26. private IDictionary _options;
  27. #endregion
  28. #region Constructors
  29. public CommandLine(string[] args)
  30. {
  31. ArrayList list = new ArrayList();
  32. for (int i = 0; i < args.Length; i++)
  33. {
  34. char ch = args[i][0];
  35. if ((ch != '/') && (ch != '-'))
  36. {
  37. list.Add(args[i]);
  38. }
  39. else
  40. {
  41. int index = args[i].IndexOf(':');
  42. if (index == -1)
  43. {
  44. string strA = args[i].Substring(1);
  45. if ((string.Compare(strA, "help", StringComparison.OrdinalIgnoreCase) == 0) || strA.Equals("?"))
  46. {
  47. _showHelp = true;
  48. }
  49. else
  50. {
  51. Options[strA] = string.Empty;
  52. }
  53. }
  54. else
  55. {
  56. Options[args[i].Substring(1, index - 1)] = args[i].Substring(index + 1);
  57. }
  58. }
  59. }
  60. _arguments = (string[]) list.ToArray(typeof (string));
  61. }
  62. #endregion
  63. #region Properties
  64. public string[] Arguments
  65. {
  66. get { return _arguments; }
  67. }
  68. public IDictionary Options
  69. {
  70. get
  71. {
  72. if (_options == null)
  73. {
  74. _options = new HybridDictionary(true);
  75. }
  76. return _options;
  77. }
  78. }
  79. public bool ShowHelp
  80. {
  81. get { return _showHelp; }
  82. }
  83. #endregion
  84. }
  85. }