PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net/Attributes/NullableArgumentAttribute.cs

#
C# | 77 lines | 44 code | 6 blank | 27 comment | 5 complexity | 5e7a2d495c1ff6969bc8cf572022297e MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. namespace Mercurial.Attributes
  3. {
  4. /// <summary>
  5. /// This attribute can be applied to nullable properties in option classes,
  6. /// to specify the option to pass to the executable in case
  7. /// of a <c>null</c> value, or the option to pass before the property
  8. /// value itself.
  9. /// </summary>
  10. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  11. public sealed class NullableArgumentAttribute : ArgumentAttribute
  12. {
  13. /// <summary>
  14. /// Gets or sets the option to pass to the Mercurial executable if the
  15. /// property value is <c>null</c>. If <see cref="string.Empty"/>,
  16. /// no option will be passed in this case.
  17. /// </summary>
  18. public string NullOption
  19. {
  20. get;
  21. set;
  22. }
  23. /// <summary>
  24. /// Gets or sets the option to specify before the property value when
  25. /// passing it to the Mercurial executable. If <see cref="string.Empty"/>,
  26. /// only the property value itself will be passed.
  27. /// </summary>
  28. public string NonNullOption
  29. {
  30. get;
  31. set;
  32. }
  33. /// <summary>
  34. /// Gets a collection of options or arguments to pass to the Mercurial
  35. /// executable, based on the property value from the options class.
  36. /// </summary>
  37. /// <param name="propertyValue">
  38. /// The property value from the tagged property of the options class.
  39. /// </param>
  40. /// <returns>
  41. /// A collection of options or arguments, or an empty array or <c>null</c>
  42. /// for no options for the specified property value.
  43. /// </returns>
  44. public override string[] GetOptions(object propertyValue)
  45. {
  46. string result;
  47. if (propertyValue == null)
  48. result = null;
  49. else
  50. result = propertyValue.ToString().Trim();
  51. if (StringEx.IsNullOrWhiteSpace(result))
  52. {
  53. if (StringEx.IsNullOrWhiteSpace(NullOption))
  54. return null;
  55. return new[]
  56. {
  57. NullOption
  58. };
  59. }
  60. if (!StringEx.IsNullOrWhiteSpace(NonNullOption))
  61. return new[]
  62. {
  63. NonNullOption, "\"" + result + "\""
  64. };
  65. return new[]
  66. {
  67. "\"" + result + "\""
  68. };
  69. }
  70. }
  71. }