PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BTSControl/Microsoft.BizTalk.ApplicationDeployment.CommandLine/CommandLineParser.cs

#
C# | 195 lines | 189 code | 5 blank | 1 comment | 64 complexity | b4ff5c0f50c58a7ce93ec3041bff5946 MD5 | raw file
  1. namespace Microsoft.BizTalk.ApplicationDeployment.CommandLine
  2. {
  3. using Microsoft.BizTalk.ApplicationDeployment;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. internal static class CommandLineParser
  12. {
  13. public static List<CommandLineArgumentException> Expand(ref NameValueCollection nameValueArgs, CommandLineArgDescriptorList commandLineArgDescriptorList)
  14. {
  15. if (commandLineArgDescriptorList == null)
  16. {
  17. throw new ArgumentNullException("commandLineArgDescriptorList");
  18. }
  19. List<CommandLineArgumentException> list = new List<CommandLineArgumentException>();
  20. NameValueCollection values = new NameValueCollection();
  21. for (int i = 0; i < nameValueArgs.Count; i++)
  22. {
  23. string key = nameValueArgs.GetKey(i);
  24. string[] strArray = nameValueArgs.GetValues(i);
  25. string name = null;
  26. if (key != null)
  27. {
  28. foreach (CommandLineArgDescriptor descriptor in commandLineArgDescriptorList)
  29. {
  30. if (descriptor.Named)
  31. {
  32. string strA = descriptor.Name;
  33. if (strA.StartsWith(key, StringComparison.OrdinalIgnoreCase))
  34. {
  35. if (name == null)
  36. {
  37. name = strA;
  38. if (string.Compare(strA, key, StringComparison.OrdinalIgnoreCase) != 0)
  39. {
  40. continue;
  41. }
  42. }
  43. else
  44. {
  45. string message = CommandLineResources.GetString(CommandLineResources.ResourceID.AmbiguousNamedArgument);
  46. list.Add(new CommandLineArgumentException(message, key, TraceLevel.Error));
  47. }
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. if (name != null)
  54. {
  55. if (strArray != null)
  56. {
  57. foreach (string str5 in strArray)
  58. {
  59. values.Add(name, str5);
  60. }
  61. }
  62. else
  63. {
  64. values.Add(name, null);
  65. }
  66. //Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Named argument '{0}' expanded to '{1}'.", new object[] { key, name }), new object[0]);
  67. }
  68. else if (strArray != null)
  69. {
  70. foreach (string str7 in strArray)
  71. {
  72. values.Add(key, str7);
  73. }
  74. }
  75. else
  76. {
  77. values.Add(key, null);
  78. }
  79. }
  80. nameValueArgs = values;
  81. return list;
  82. }
  83. public static NameValueCollection Parse(string[] args)
  84. {
  85. NameValueCollection values = new NameValueCollection();
  86. if (args != null)
  87. {
  88. foreach (string str in args)
  89. {
  90. string str2;
  91. string str3;
  92. ParseArg(str, out str2, out str3);
  93. values.Add(str2, str3);
  94. }
  95. }
  96. return values;
  97. }
  98. private static void ParseArg(string arg, out string name, out string value)
  99. {
  100. if (arg == null)
  101. {
  102. throw new ArgumentNullException("arg");
  103. }
  104. name = null;
  105. value = null;
  106. if (!arg.StartsWith("-", StringComparison.Ordinal) && !arg.StartsWith("/", StringComparison.Ordinal))
  107. {
  108. value = arg;
  109. }
  110. else
  111. {
  112. StringBuilder builder = new StringBuilder();
  113. for (int i = 1; i < arg.Length; i++)
  114. {
  115. char c = arg[i];
  116. if (!char.IsLetterOrDigit(c) && (c != '?'))
  117. {
  118. break;
  119. }
  120. builder.Append(c);
  121. }
  122. name = builder.ToString();
  123. arg = arg.Remove(0, 1 + name.Length);
  124. if (arg == "+")
  125. {
  126. value = "+";
  127. }
  128. else if (arg == "-")
  129. {
  130. value = "-";
  131. }
  132. else if (arg.StartsWith(":", StringComparison.Ordinal) || arg.StartsWith("=", StringComparison.Ordinal))
  133. {
  134. value = arg.Substring(1);
  135. }
  136. }
  137. }
  138. public static List<CommandLineArgumentException> Validate(NameValueCollection nameValueArgs, CommandLineArgDescriptorList commandLineArgDescriptorList)
  139. {
  140. if (commandLineArgDescriptorList == null)
  141. {
  142. throw new ArgumentNullException("commandLineArgDescriptorList");
  143. }
  144. List<CommandLineArgumentException> list = new List<CommandLineArgumentException>();
  145. foreach (CommandLineArgDescriptor descriptor in commandLineArgDescriptorList)
  146. {
  147. string[] values = nameValueArgs.GetValues(descriptor.Named ? descriptor.Name : null);
  148. int num = (values == null) ? 0 : values.Length;
  149. if (num < descriptor.MinOccurs)
  150. {
  151. CommandLineArgumentException item = new CommandLineArgumentException(CommandLineResources.GetFormattedString(CommandLineResources.ResourceID.RequiredArgumentNotSpecified, new object[] { descriptor.MinOccurs.ToString(CultureInfo.InvariantCulture) }), descriptor.Name, TraceLevel.Error);
  152. list.Add(item);
  153. }
  154. else if (num > descriptor.MaxOccurs)
  155. {
  156. CommandLineArgumentException exception2 = new CommandLineArgumentException(CommandLineResources.GetFormattedString(CommandLineResources.ResourceID.ExtraArgumentsSpecified, new object[] { descriptor.MaxOccurs.ToString(CultureInfo.InvariantCulture) }), descriptor.Name, TraceLevel.Error);
  157. list.Add(exception2);
  158. }
  159. }
  160. for (int i = 0; i < nameValueArgs.Count; i++)
  161. {
  162. string key = nameValueArgs.GetKey(i);
  163. string[] strArray2 = nameValueArgs.GetValues(i);
  164. if (key != null)
  165. {
  166. CommandLineArgDescriptor descriptor2 = commandLineArgDescriptorList.GetDescriptor(key);
  167. if (descriptor2 == null)
  168. {
  169. CommandLineArgumentException exception3 = new CommandLineArgumentException(CommandLineResources.GetString(CommandLineResources.ResourceID.UnrecognizedNamedArgument), key, TraceLevel.Error);
  170. list.Add(exception3);
  171. }
  172. else if (descriptor2.Type == CommandLineArgDescriptor.ArgumentType.Simple)
  173. {
  174. if ((strArray2 != null) && (strArray2.Length > 0))
  175. {
  176. CommandLineArgumentException exception4 = new CommandLineArgumentException(CommandLineResources.GetString(CommandLineResources.ResourceID.ValueForSimpleArgument), key, TraceLevel.Error);
  177. list.Add(exception4);
  178. }
  179. }
  180. else if ((descriptor2.Type == CommandLineArgDescriptor.ArgumentType.Boolean) && ((strArray2 == null) || (((strArray2 != null) && (strArray2[0] != "+")) && (strArray2[0] != "-"))))
  181. {
  182. CommandLineArgumentException exception5 = new CommandLineArgumentException(CommandLineResources.GetString(CommandLineResources.ResourceID.NoFlagForBooleanArgument), key, TraceLevel.Error);
  183. list.Add(exception5);
  184. }
  185. }
  186. }
  187. return list;
  188. }
  189. }
  190. }