PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/WPFImage/TagPrinter/ArgumentParser.cs

#
C# | 433 lines | 306 code | 30 blank | 97 comment | 53 complexity | 61dc2a9b39d6fcbea17d47bd1cffe6dd MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BitmapTagger;
  6. namespace TagPrinter
  7. {
  8. /// <summary>
  9. /// Parsers and stores the application arguments
  10. /// </summary>
  11. public class ArgumentParser
  12. {
  13. /// <summary>
  14. /// Create a new Argument Parser
  15. /// </summary>
  16. /// <param name="args">The application arguments to parse and store</param>
  17. public ArgumentParser(string[] args)
  18. {
  19. SetDefaults();
  20. Arguments = args;
  21. Parse();
  22. }
  23. /// <summary>
  24. /// Parse the arguments
  25. /// </summary>
  26. private void Parse()
  27. {
  28. List<string> argumentList = Arguments.ToList<string>();
  29. string argument;
  30. bool conversionWorked;
  31. bool booleanValue;
  32. TagPrecedence precedence;
  33. while (argumentList.Count > 0)
  34. {
  35. argument = argumentList[0];
  36. argumentList.RemoveAt(0);
  37. if (IsLoadExifOption(argument))
  38. {
  39. if (argumentList.Count > 0)
  40. {
  41. booleanValue = ConvertTrueFalseStrings(argumentList[0],
  42. out conversionWorked);
  43. if (conversionWorked)
  44. {
  45. LoadExif = booleanValue;
  46. argumentList.RemoveAt(0);
  47. }
  48. else
  49. Console.WriteLine("Load Exif option must be followed by t or f");
  50. }
  51. else
  52. Console.WriteLine("Load Exif option must be followed by t or f");
  53. }
  54. else if (IsLoadXMPExifOption(argument))
  55. {
  56. if (argumentList.Count > 0)
  57. {
  58. booleanValue = ConvertTrueFalseStrings(argumentList[0],
  59. out conversionWorked);
  60. if (conversionWorked)
  61. {
  62. LoadXMPExif = booleanValue;
  63. argumentList.RemoveAt(0);
  64. }
  65. else
  66. Console.WriteLine("Load XMPExif option must be followed by t or f");
  67. }
  68. else
  69. Console.WriteLine("Load XMPExif option must be followed by t or f");
  70. }
  71. else if (IsLoadXMPOption(argument))
  72. {
  73. if (argumentList.Count > 0)
  74. {
  75. booleanValue = ConvertTrueFalseStrings(argumentList[0],
  76. out conversionWorked);
  77. if (conversionWorked)
  78. {
  79. LoadXMP = booleanValue;
  80. argumentList.RemoveAt(0);
  81. }
  82. else
  83. Console.WriteLine("Load XMP option must be followed by t or f");
  84. }
  85. else
  86. Console.WriteLine("Load XMP option must be followed by t or f");
  87. }
  88. else if (IsPrecedenceOption(argument))
  89. {
  90. if (argumentList.Count > 0)
  91. {
  92. precedence = ConvertPrecedenceStrings(argumentList[0],
  93. out conversionWorked);
  94. if (conversionWorked)
  95. {
  96. Precedence = precedence;
  97. argumentList.RemoveAt(0);
  98. }
  99. else
  100. Console.WriteLine("Tag Precedence option must be followed by a valid value, to see a listing of valid values use the help option . -h");
  101. }
  102. else
  103. Console.WriteLine("Tag Precedence option must be followed by a valid value, to see a listing of valid values use the help option . -h");
  104. }
  105. else if (IsValidFilePath(argument))
  106. {
  107. FilePaths.Add(argument);
  108. }
  109. else if (IsHelpOption(argument))
  110. {
  111. HelpAsked = true;
  112. }
  113. else
  114. {
  115. Console.WriteLine(argument + " is a not a valid option, use -h for help");
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Convert the precedence argument strings to TagPrecedence
  121. /// </summary>
  122. /// <param name="arg">The argument to convert</param>
  123. /// <param name="worked">Whether the conversion worked</param>
  124. /// <returns>The value of the argument converted, always ExifXMPExifXMP if the
  125. /// conversion did not work</returns>
  126. private TagPrecedence ConvertPrecedenceStrings(string arg, out bool worked)
  127. {
  128. worked = false;
  129. if (!string.IsNullOrEmpty(arg))
  130. {
  131. string normalized = arg.ToLower();
  132. if (normalized.Equals("e-xe-x"))
  133. {
  134. worked = true;
  135. return TagPrecedence.Exif_XMPExif_XMP;
  136. }
  137. else if (normalized.Equals("e-x-xe"))
  138. {
  139. worked = true;
  140. return TagPrecedence.Exif_XMP_XMPExif;
  141. }
  142. else if (normalized.Equals("xe-e-x"))
  143. {
  144. worked = true;
  145. return TagPrecedence.XMPExif_Exif_XMP;
  146. }
  147. else if (normalized.Equals("xe-x-e"))
  148. {
  149. worked = true;
  150. return TagPrecedence.XMPExif_XMP_Exif;
  151. }
  152. else if (normalized.Equals("x-e-xe"))
  153. {
  154. worked = true;
  155. return TagPrecedence.XMP_Exif_XMPExif;
  156. }
  157. else if (normalized.Equals("x-xe-e"))
  158. {
  159. worked = true;
  160. return TagPrecedence.XMP_XMPExif_Exif;
  161. }
  162. }
  163. return TagPrecedence.Exif_XMPExif_XMP;
  164. }
  165. /// <summary>
  166. /// Convert the true and false argument strings to bools
  167. /// </summary>
  168. /// <param name="arg">The argument to convert</param>
  169. /// <param name="worked">Whether the conversion worked</param>
  170. /// <returns>The value of the argument converted, always true if the
  171. /// conversion did not work</returns>
  172. private bool ConvertTrueFalseStrings(string arg, out bool worked)
  173. {
  174. worked = false;
  175. if (!string.IsNullOrEmpty(arg))
  176. {
  177. string normalized = arg.ToLower();
  178. if (normalized.Equals("t") || normalized.Equals("true"))
  179. {
  180. worked = true;
  181. return true;
  182. }
  183. else if (normalized.Equals("f") || normalized.Equals("false"))
  184. {
  185. worked = true;
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. /// <summary>
  192. /// Get whether the argument is the string for the help option
  193. /// </summary>
  194. /// <param name="arg">The argument to check</param>
  195. /// <returns>True if it is, false if not</returns>
  196. private bool IsHelpOption(string arg)
  197. {
  198. if (!string.IsNullOrEmpty(arg))
  199. {
  200. string normalized = arg.ToLower();
  201. if (normalized.Equals("-h") || normalized.Equals("-help"))
  202. {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. /// <summary>
  209. /// Get whether the argument is the string for the load Exif option
  210. /// </summary>
  211. /// <param name="arg">The argument to check</param>
  212. /// <returns>True if it is, false if not</returns>
  213. private bool IsLoadExifOption(string arg)
  214. {
  215. if (!string.IsNullOrEmpty(arg))
  216. {
  217. string normalized = arg.ToLower();
  218. if (normalized.Equals("-e") || normalized.Equals("-exif"))
  219. {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. /// <summary>
  226. /// Get whether the argument is the string for the load XMPExif option
  227. /// </summary>
  228. /// <param name="arg">The argument to check</param>
  229. /// <returns>True if it is, false if not</returns>
  230. private bool IsLoadXMPExifOption(string arg)
  231. {
  232. if (!string.IsNullOrEmpty(arg))
  233. {
  234. string normalized = arg.ToLower();
  235. if (normalized.Equals("-xe") || normalized.Equals("-xmpexif"))
  236. {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. /// <summary>
  243. /// Get whether the argument is the string for the load XMP option
  244. /// </summary>
  245. /// <param name="arg">The argument to check</param>
  246. /// <returns>True if it is, false if not</returns>
  247. private bool IsLoadXMPOption(string arg)
  248. {
  249. if (!string.IsNullOrEmpty(arg))
  250. {
  251. string normalized = arg.ToLower();
  252. if (normalized.Equals("-x") || normalized.Equals("-xmp"))
  253. {
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. /// <summary>
  260. /// Get whether the argument is the string for the precedence option
  261. /// </summary>
  262. /// <param name="arg">The argument to check</param>
  263. /// <returns>True if it is, false if not</returns>
  264. private bool IsPrecedenceOption(string arg)
  265. {
  266. if (!string.IsNullOrEmpty(arg))
  267. {
  268. string normalized = arg.ToLower();
  269. if (normalized.Equals("-p") || normalized.Equals("-precedence"))
  270. {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. /// <summary>
  277. /// Get whether the argument is a valid file path
  278. /// </summary>
  279. /// <param name="arg">The argument to check</param>
  280. /// <returns>True if it is, false if not</returns>
  281. private bool IsValidFilePath(string arg)
  282. {
  283. return System.IO.File.Exists(arg);
  284. }
  285. /// <summary>
  286. /// Set the default values
  287. /// </summary>
  288. private void SetDefaults()
  289. {
  290. LoadExif = true;
  291. LoadXMPExif = true;
  292. LoadXMP = true;
  293. Precedence = TagPrecedence.Exif_XMPExif_XMP;
  294. FilePaths = new List<string>();
  295. HelpAsked = false;
  296. }
  297. /// <summary>
  298. /// Get or set the arguments
  299. /// </summary>
  300. private string[] Arguments
  301. {
  302. get;
  303. set;
  304. }
  305. /// <summary>
  306. /// Get the collection of file paths to open
  307. /// </summary>
  308. public List<string> FilePaths
  309. {
  310. get;
  311. private set;
  312. }
  313. /// <summary>
  314. /// Get whether or not any file paths were passed in the
  315. /// application arguments
  316. /// </summary>
  317. public bool HasFilePaths
  318. {
  319. get
  320. {
  321. return FilePaths.Count > 0;
  322. }
  323. }
  324. /// <summary>
  325. /// Get whether or not to load Exif tags
  326. /// </summary>
  327. /// <remarks>
  328. /// Defaults to true
  329. /// </remarks>
  330. public bool LoadExif
  331. {
  332. get;
  333. private set;
  334. }
  335. /// <summary>
  336. /// Get whether or not to load XMP Exif tags
  337. /// </summary>
  338. /// <remarks>
  339. /// Defaults to true
  340. /// </remarks>
  341. public bool LoadXMPExif
  342. {
  343. get;
  344. private set;
  345. }
  346. /// <summary>
  347. /// Get whether or not to load XMP tags
  348. /// </summary>
  349. /// <remarks>
  350. /// Defaults to true
  351. /// </remarks>
  352. public bool LoadXMP
  353. {
  354. get;
  355. private set;
  356. }
  357. /// <summary>
  358. /// Get the tag precedence
  359. /// </summary>
  360. /// <remarks>
  361. /// Defaults to Exif > XMPExif > XMP
  362. /// </remarks>
  363. public TagPrecedence Precedence
  364. {
  365. get;
  366. private set;
  367. }
  368. /// <summary>
  369. /// Get whether or not help was asked in the arguments
  370. /// </summary>
  371. public bool HelpAsked
  372. {
  373. get;
  374. private set;
  375. }
  376. /// <summary>
  377. /// Get the help string
  378. /// </summary>
  379. public string HelpString
  380. {
  381. get
  382. {
  383. string help = "Tag Printer options: \n";
  384. help += "-e Whether to load Exif tags, followed by 't' or 'f'. Defaults to true.\n";
  385. help += "-xe Whether to load XMPExif tags, followed by 't' or 'f'. Defaults to true. \n";
  386. help += "-x Whether to load XMP tags, followed by 't' or 'f'. Defaults to true. \n";
  387. help += "FILE PATH A file path to an image to read. For example \"C:\\Users\\User\\Desktop\\image.png\" \n";
  388. help += "-p The tag precedence, used in resolving conflicts between common tags in the different tag systems. Possible values include: \n";
  389. help += " e-xe-x Exif > XMPExif > XMP \n";
  390. help += " e-x-xe Exif > XMP > XMPExif \n";
  391. help += " xe-e-x XMPExif > Exif > XMP \n";
  392. help += " xe-x-e XMPExif > XMP > Exif \n";
  393. help += " x-e-xe XMP > Exif > XMPExif \n";
  394. help += " x-xe-e XMP > XMPExif > Exif \n";
  395. help += " Defaults to Exif > XMPExif > XMP \n";
  396. help += "-h Help \n";
  397. help += "Order or arugments does not matter. \n";
  398. help += "There can as a many file path arguments as you want, but there must be at least one.";
  399. return help;
  400. }
  401. }
  402. }
  403. }