PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net/Attributes/EnumArgumentAttribute.cs

#
C# | 137 lines | 73 code | 13 blank | 51 comment | 11 complexity | 23cf957e570a55f6757f95705a789825 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Globalization;
  3. namespace Mercurial.Attributes
  4. {
  5. /// <summary>
  6. /// This attribute can be applied to enum-properties in option classes,
  7. /// to specify the arguments to pass to the executable in case of
  8. /// each possible enum value for the property. Note that usage of any enum values that
  9. /// don't have a corresponding <see cref="EnumArgumentAttribute"/> will be ignored.
  10. /// </summary>
  11. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
  12. public sealed class EnumArgumentAttribute : ArgumentAttribute
  13. {
  14. /// <summary>
  15. /// This is the backing field for the <see cref="Argument1"/> and <see cref="Argument2"/> properties.
  16. /// </summary>
  17. private readonly string[] _Arguments = new string[0];
  18. /// <summary>
  19. /// This is the backing field for the <see cref="Value"/> property.
  20. /// </summary>
  21. private readonly object _Value;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="EnumArgumentAttribute"/> class
  24. /// with one argument for the enum value.
  25. /// </summary>
  26. /// <param name="value">The enum value.</param>
  27. /// <param name="argument1">The argument.</param>
  28. public EnumArgumentAttribute(object value, string argument1)
  29. {
  30. _Value = value;
  31. _Arguments = new[]
  32. {
  33. argument1
  34. };
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="EnumArgumentAttribute"/> class
  38. /// with two arguments for the enum value.
  39. /// </summary>
  40. /// <param name="value">The enum value.</param>
  41. /// <param name="argument1">The first argument.</param>
  42. /// <param name="argument2">The second argument.</param>
  43. public EnumArgumentAttribute(object value, string argument1, string argument2)
  44. {
  45. _Value = value;
  46. _Arguments = new[]
  47. {
  48. argument1, argument2
  49. };
  50. }
  51. /// <summary>
  52. /// Gets the enum value to map the arguments to.
  53. /// </summary>
  54. public object Value
  55. {
  56. get
  57. {
  58. return _Value;
  59. }
  60. }
  61. /// <summary>
  62. /// Gets the first argument that the <see cref="Value"/> is mapped to,
  63. /// or <see cref="string.Empty"/> if there is no argument.
  64. /// </summary>
  65. public string Argument1
  66. {
  67. get
  68. {
  69. if (_Arguments != null && _Arguments.Length >= 1)
  70. return _Arguments[0];
  71. return string.Empty;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets the second argument that the <see cref="Value"/> is mapped to,
  76. /// or <see cref="string.Empty"/> if there is no second argument.
  77. /// </summary>
  78. public string Argument2
  79. {
  80. get
  81. {
  82. if (_Arguments != null && _Arguments.Length >= 2)
  83. return _Arguments[1];
  84. return string.Empty;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets a value indicating whether the enum value is a bitmask value (see <see cref="FlagsAttribute"/>)
  89. /// or not.
  90. /// </summary>
  91. public bool IsBitmask
  92. {
  93. get;
  94. set;
  95. }
  96. /// <summary>
  97. /// Gets a collection of options or arguments to pass to the Mercurial
  98. /// executable, based on the property value from the command class.
  99. /// </summary>
  100. /// <param name="propertyValue">
  101. /// The property value from the tagged property of the command class.
  102. /// </param>
  103. /// <returns>
  104. /// A collection of options or arguments, or an empty array or <c>null</c>
  105. /// for no options for the specified property value.
  106. /// </returns>
  107. public override string[] GetOptions(object propertyValue)
  108. {
  109. int propertyValueAsNumber = Convert.ToInt32(propertyValue, CultureInfo.InvariantCulture);
  110. int valueAsNumber = Convert.ToInt32(_Value, CultureInfo.InvariantCulture);
  111. if (IsBitmask)
  112. {
  113. if ((propertyValueAsNumber & valueAsNumber) == valueAsNumber)
  114. return _Arguments;
  115. }
  116. else
  117. {
  118. if (propertyValueAsNumber == valueAsNumber)
  119. return _Arguments;
  120. }
  121. return null;
  122. }
  123. }
  124. }