Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5. namespace Broargs
  6. {
  7. public static class Parser
  8. {
  9. private static string enableArgs = "";
  10. private static string disableArgs = "";
  11. private static string otherArgs = "";
  12.  
  13.  
  14. // Собирает корректную строку аргументов
  15. public static string Parse(string stringForParse)
  16. {
  17. // Разносит значения аргументов по заранее подготовленным массивам
  18. Decompose(stringForParse);
  19.  
  20. string result = "";
  21.  
  22. if (enableArgs.Length > 0)
  23. {
  24. result += "--enable-features=" + enableArgs;
  25. }
  26. if (disableArgs.Length > 0)
  27. {
  28. result += " --disable-features=" + disableArgs;
  29. }
  30.  
  31. return (result + " " + otherArgs).Trim();
  32. }
  33.  
  34.  
  35. // Разносит значения аргументов по заранее подготовленным массивам
  36. private static void Decompose(string stringForParse)
  37. {
  38.  
  39. // Разделяет строку на отдельные состаляющие в массив
  40. string[] arr = Prepare(stringForParse);
  41. List<string> enableList = new List<string>();
  42. List<string> disableList = new List<string>();
  43. List<string> otherList = new List<string>();
  44. foreach (string elem in arr)
  45. {
  46. if (elem.Contains("--enable-features="))
  47. {
  48. int beginIndex = elem.IndexOf("=") + 1;
  49. enableList.Add(elem.Substring(beginIndex));
  50. }
  51. else if (elem.Contains("--disable-features="))
  52. {
  53. int beginIndex = elem.IndexOf("=") + 1;
  54. disableList.Add(elem.Substring(beginIndex));
  55. }
  56. else
  57. {
  58. otherList.Add(elem);
  59. }
  60. }
  61.  
  62. enableArgs = string.Join(",", enableList);
  63. disableArgs = string.Join(",", disableList);
  64. otherArgs = string.Join(" ", otherList);
  65. }
  66.  
  67. // Разделяет строку на отдельные состаляющие в массив
  68. private static string[] Prepare(string stringForParse)
  69. {
  70.  
  71. // Полагаю это строчка лишняя
  72. stringForParse = new Regex(@"\s*--").Replace(stringForParse, " --");
  73.  
  74. Regex pattern = new Regex("--\\S*");
  75. // Разделяем строку на отдельные элементы
  76. MatchCollection matches = pattern.Matches(stringForParse);
  77.  
  78. // Последующий код переводит MatchCollection в обычный Array и возвращает его
  79. // Походу можно написать что-то типа return matches.ToArray(), но проверять неохота
  80. List<string> list = new List<string>();
  81. foreach (Match match in matches)
  82. {
  83. list.Add(match.ToString());
  84. }
  85.  
  86. return list.ToArray();
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement