ArgumentParser.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. namespace De.Thekid.INotify
  6. {
  7. /// See also <a href="http://linux.die.net/man/1/inotifywait">inotifywait(1) - Linux man page</a>
  8. public class ArgumentParser
  9. {
  10. /// Helper method for parser
  11. protected string Value(string[] args, int i, string name)
  12. {
  13. if (i > args.Length)
  14. {
  15. throw new ArgumentException("Argument " + name + " requires a value");
  16. }
  17. return args[i];
  18. }
  19. /// Tokenizes "printf" format string into an array of strings
  20. protected string[] TokenizeFormat(string arg)
  21. {
  22. var result = new List<string>();
  23. var tokens = arg.Split(new char[]{ '%' });
  24. foreach (var token in tokens)
  25. {
  26. if (token.Length == 0) continue;
  27. if ("efwT".IndexOf(token[0]) != -1)
  28. {
  29. result.Add(token[0].ToString());
  30. if (token.Length > 1)
  31. {
  32. result.Add(token.Substring(1));
  33. }
  34. }
  35. else
  36. {
  37. result.Add(token);
  38. }
  39. }
  40. return result.ToArray();
  41. }
  42. private void ParseArgument(string option, string[] args, ref int i, Arguments result)
  43. {
  44. if ("--recursive" == option || "-r" == option)
  45. {
  46. result.Recursive = true;
  47. }
  48. else if ("--monitor" == option || "-m" == option)
  49. {
  50. result.Monitor = true;
  51. }
  52. else if ("--quiet" == option || "-q" == option)
  53. {
  54. result.Quiet = true;
  55. }
  56. else if ("--event" == option || "-e" == option)
  57. {
  58. result.Events = new List<string>(Value(args, ++i, "event").Split(','));
  59. }
  60. else if ("--format" == option)
  61. {
  62. result.Format = TokenizeFormat(Value(args, ++i, "format"));
  63. }
  64. else if ("--exclude" == option)
  65. {
  66. result.Exclude = new Regex(Value(args, ++i, "exclude"));
  67. }
  68. else if ("--excludei" == option)
  69. {
  70. result.Exclude = new Regex(Value(args, ++i, "exclude"), RegexOptions.IgnoreCase);
  71. }
  72. else if (option.StartsWith("--event="))
  73. {
  74. result.Events = new List<string>(option.Split(new Char[]{'='}, 2)[1].Split(','));
  75. }
  76. else if (option.StartsWith("--format="))
  77. {
  78. result.Format = TokenizeFormat(option.Split(new Char[]{'='}, 2)[1]);
  79. }
  80. else if (option.StartsWith("--exclude="))
  81. {
  82. result.Exclude = new Regex(option.Split(new Char[]{'='}, 2)[1]);
  83. }
  84. else if (option.StartsWith("--excludei="))
  85. {
  86. result.Exclude = new Regex(option.Split(new Char[]{'='}, 2)[1], RegexOptions.IgnoreCase);
  87. }
  88. else if (Directory.Exists(option))
  89. {
  90. result.Paths.Add(System.IO.Path.GetFullPath(option));
  91. }
  92. }
  93. /// Creates a new argument parser and parses the arguments
  94. public Arguments Parse(string[] args)
  95. {
  96. var result = new Arguments();
  97. for (var i = 0; i < args.Length; i++)
  98. {
  99. if (!args[i].StartsWith("--") && args[i].StartsWith("-") && args[i].Length > 2)
  100. {
  101. string options = args[i];
  102. for (var j = 1; j < options.Length; j++)
  103. {
  104. ParseArgument("-" + options.Substring(j, 1), args, ref i, result);
  105. }
  106. }
  107. else
  108. {
  109. ParseArgument(args[i], args, ref i, result);
  110. }
  111. }
  112. return result;
  113. }
  114. /// Usage
  115. public void PrintUsage(string name, TextWriter writer)
  116. {
  117. writer.WriteLine("Usage: " + name + " [options] path [...]");
  118. writer.WriteLine();
  119. writer.WriteLine("Options:");
  120. writer.WriteLine("-r/--recursive: Recursively watch all files and subdirectories inside path");
  121. writer.WriteLine("-m/--monitor: Keep running until killed (e.g. via Ctrl+C)");
  122. writer.WriteLine("-q/--quiet: Do not output information about actions");
  123. writer.WriteLine("-e/--event list: Which events (create, modify, delete, move) to watch, comma-separated. Default: all");
  124. writer.WriteLine("--format format: Format string for output.");
  125. writer.WriteLine("--exclude: Do not process any events whose filename matches the specified regex");
  126. writer.WriteLine("--excludei: Ditto, case-insensitive");
  127. writer.WriteLine();
  128. writer.WriteLine("Formats:");
  129. writer.WriteLine("%e : Event name");
  130. writer.WriteLine("%f : File name");
  131. writer.WriteLine("%w : Path name");
  132. writer.WriteLine("%T : Current date and time");
  133. }
  134. }
  135. }