AbstractHandler.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 abstract IList<Setting> Settings
  28. {
  29. get;
  30. }
  31. public Setting Setting
  32. {
  33. get
  34. {
  35. return Settings.Single(setting => setting.handler);
  36. }
  37. }
  38. protected string FindInPath(string program)
  39. {
  40. foreach (string location in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
  41. {
  42. string path = Path.Combine(location, program);
  43. if (File.Exists(path))
  44. return path;
  45. }
  46. return null;
  47. }
  48. protected MatchOption SetValue(Match match, string groupName, ref string value)
  49. {
  50. Group group = match.Groups[groupName];
  51. if (group.Success)
  52. value = group.Value;
  53. return MatchOption.Set;
  54. }
  55. protected MatchOption SetBooleanValue(Match match, string groupName, out bool option, ref string value)
  56. {
  57. Group group = match.Groups[groupName];
  58. if (group.Success)
  59. switch (group.Value.ToLowerInvariant())
  60. {
  61. case "yes":
  62. option = true;
  63. break;
  64. case "no":
  65. option = false;
  66. break;
  67. default:
  68. value = group.Value;
  69. goto case "yes";
  70. }
  71. else
  72. option = true;
  73. return MatchOption.Option;
  74. }
  75. protected MatchOption SetYesNoValue(Match match, string groupName, out AutoYesNoOption option, ref string value)
  76. {
  77. bool optionValue;
  78. MatchOption matchOption = SetBooleanValue(match, groupName, out optionValue, ref value);
  79. option = optionValue ? AutoYesNoOption.Yes : AutoYesNoOption.No;
  80. return matchOption;
  81. }
  82. protected void AddArguments(List<string> command, params object[] arguments)
  83. {
  84. command.AddRange(arguments.Select(argument => argument.ToString()));
  85. }
  86. }