PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ZebraFlickr/CommandLine/EnumArgumentDecoder.cs

https://bitbucket.org/garethl/zebraflickr
C# | 161 lines | 91 code | 33 blank | 37 comment | 13 complexity | 0c2ee37c67630c61c0e31c77683a108f MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. using System.Text;
  33. namespace ZebraFlickr.CommandLine
  34. {
  35. public class EnumArgumentDecoder : IArgumentDecoder
  36. {
  37. private ArgumentMap _map;
  38. #region Implementation of IArgumentDecoder
  39. /// <summary>
  40. /// Decode an argument
  41. /// </summary>
  42. /// <param name="item">Item to set the argument on</param>
  43. /// <param name="argument">The argument</param>
  44. public void Decode(object item, Parser.Argument argument)
  45. {
  46. var enumType = _map.Property.PropertyType;
  47. var enumValue = Enum.Parse(enumType, argument.Value, true);
  48. _map.Property.SetValue(item, enumValue, null);
  49. }
  50. /// <summary>
  51. /// Initialize the decoder with the map
  52. /// </summary>
  53. /// <param name="map">The argument map</param>
  54. public void Initialize(ArgumentMap map)
  55. {
  56. _map = map;
  57. var enumType = _map.Property.PropertyType;
  58. var names = GetNames(enumType);
  59. var defaultDescription = "";
  60. if (map.Default != null)
  61. {
  62. var values = Enum.GetValues(enumType);
  63. for (int x = 0; x < values.Length; x++)
  64. {
  65. if (values.GetValue(x).Equals(map.Default))
  66. {
  67. defaultDescription = names[x];
  68. break;
  69. }
  70. }
  71. if (!string.IsNullOrEmpty(defaultDescription))
  72. {
  73. defaultDescription = ". Default = " + defaultDescription;
  74. }
  75. }
  76. var descriptionAddition = string.Format(" ({0}){1}", FancyJoin(", ", " or ", names), defaultDescription);
  77. _map.Description += descriptionAddition;
  78. }
  79. /// <summary>
  80. /// Did the decoder find a value?
  81. /// </summary>
  82. public bool HasValue
  83. {
  84. get { return true; }
  85. }
  86. private static string FancyJoin(string separator, string lastSeparator, string[] values)
  87. {
  88. if (values.Length == 0)
  89. return string.Empty;
  90. if (values.Length == 1)
  91. return values[0];
  92. var sb = new StringBuilder();
  93. for (int x = 0; x < values.Length; x++)
  94. {
  95. if (x > 0)
  96. {
  97. if (x < values.Length - 1)
  98. sb.Append(separator);
  99. else
  100. sb.Append(lastSeparator);
  101. }
  102. sb.Append(values[x]);
  103. }
  104. return sb.ToString();
  105. }
  106. private static string[] GetNames(Type type)
  107. {
  108. var ret = new List<string>();
  109. var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
  110. foreach (var field in fields)
  111. {
  112. var attribs = field.GetCustomAttributes(typeof (DescriptionAttribute), true);
  113. string name;
  114. if (attribs.Length > 0)
  115. {
  116. name = ((DescriptionAttribute) attribs[0]).Description;
  117. }
  118. else
  119. {
  120. name = field.Name;
  121. }
  122. ret.Add(name);
  123. }
  124. return ret.ToArray();
  125. }
  126. #endregion
  127. }
  128. }