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