AbstractHandler.cs 2.5 KB

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