AbstractHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Abstract Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // AbstractHandler.cs
  6. /* Copyright 2014 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Text.RegularExpressions;
  25. public abstract class AbstractHandler
  26. {
  27. public IEnumerable<string> Options
  28. {
  29. get
  30. {
  31. return Settings.Select(setting => setting.pattern);
  32. }
  33. }
  34. public IEnumerable<string> Usages
  35. {
  36. get
  37. {
  38. return Settings.Select(setting => setting.usage);
  39. }
  40. }
  41. public abstract IEnumerable<Setting> Settings
  42. {
  43. get;
  44. }
  45. public Setting Setting
  46. {
  47. get
  48. {
  49. return Settings.Single(setting => setting.handler);
  50. }
  51. }
  52. protected string FindInPath(string program)
  53. {
  54. foreach (string location in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
  55. {
  56. string path = Path.Combine(location, program);
  57. if (File.Exists(path))
  58. return path;
  59. }
  60. return null;
  61. }
  62. protected MatchOption SetValue(Match match, string groupName, ref string value)
  63. {
  64. Group group = match.Groups[groupName];
  65. if (group.Success)
  66. value = group.Value;
  67. return MatchOption.Set;
  68. }
  69. protected MatchOption SetBooleanValue(Match match, string groupName, out bool option, ref string value)
  70. {
  71. Group group = match.Groups[groupName];
  72. if (group.Success)
  73. switch (group.Value.ToLowerInvariant())
  74. {
  75. case "yes":
  76. option = true;
  77. break;
  78. case "no":
  79. option = false;
  80. break;
  81. default:
  82. value = group.Value;
  83. goto case "yes";
  84. }
  85. else
  86. option = true;
  87. return MatchOption.Option;
  88. }
  89. protected MatchOption SetYesNoValue(Match match, string groupName, out AutoYesNoOption option, ref string value)
  90. {
  91. bool optionValue;
  92. MatchOption matchOption = SetBooleanValue(match, groupName, out optionValue, ref value);
  93. option = optionValue ? AutoYesNoOption.Yes : AutoYesNoOption.No;
  94. return matchOption;
  95. }
  96. protected void AddArguments(List<string> command, params object[] arguments)
  97. {
  98. command.AddRange(arguments.Select(argument => argument.ToString()));
  99. }
  100. }