/src/ZebraFlickr/CommandLine/DefaultArgumentDecoder.cs

https://bitbucket.org/garethl/zebraflickr · C# · 112 lines · 63 code · 9 blank · 40 comment · 15 complexity · 4c4c002286fe5293d87e2571cd8519c6 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 ZebraFlickr.CommandLine.Exceptions;
  31. namespace ZebraFlickr.CommandLine
  32. {
  33. /// <summary>
  34. /// Default argument decoder
  35. /// </summary>
  36. public class DefaultArgumentDecoder : IArgumentDecoder
  37. {
  38. private ArgumentMap _map;
  39. #region IArgumentDecoder Members
  40. /// <summary>
  41. /// Decode an argument
  42. /// </summary>
  43. /// <param name="item">Item to set the argument on</param>
  44. /// <param name="argument">The argument</param>
  45. public virtual void Decode(object item, Parser.Argument argument)
  46. {
  47. Type type = _map.Property.PropertyType;
  48. if (type == typeof (string))
  49. {
  50. _map.Property.SetValue(item, argument.Value, null);
  51. }
  52. else if (type == typeof (bool))
  53. {
  54. _map.Property.SetValue(item, true, null);
  55. }
  56. else if (type == typeof (int))
  57. {
  58. int num;
  59. if (!int.TryParse(argument.Value, out num))
  60. {
  61. throw new ValidationException(string.Format("Unable to parse argument {0}", argument.Key));
  62. }
  63. _map.Property.SetValue(item, num, null);
  64. }
  65. else if (type == typeof (DateTime))
  66. {
  67. DateTime dateTime;
  68. if (DateTime.TryParse(argument.Value, out dateTime))
  69. {
  70. _map.Property.SetValue(item, dateTime, null);
  71. }
  72. else
  73. {
  74. throw new ValidationException(string.Format("Unable to parse argument {0}", argument.Key));
  75. }
  76. }
  77. else
  78. {
  79. throw new ValidationException(string.Format("Unable to parse argument {0}", argument.Key));
  80. }
  81. }
  82. /// <summary>
  83. /// Initialize the decoder with the map
  84. /// </summary>
  85. /// <param name="map">The argument map</param>
  86. public void Initialize(ArgumentMap map)
  87. {
  88. _map = map;
  89. }
  90. /// <summary>
  91. /// Did the decoder find a value?
  92. /// </summary>
  93. public bool HasValue
  94. {
  95. get
  96. {
  97. return ((_map.Property.PropertyType != typeof (bool)) &&
  98. (_map.Property.PropertyType != typeof (IList<bool>)));
  99. }
  100. }
  101. #endregion
  102. }
  103. }