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

/src/ZebraFlickr/Program.cs

https://bitbucket.org/garethl/zebraflickr
C# | 119 lines | 76 code | 17 blank | 26 comment | 7 complexity | 5ab4d70a19b1371d3b5526eff2deea68 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.Diagnostics;
  30. using System.Text;
  31. using ZebraFlickr.CommandLine;
  32. using ZebraFlickr.CommandLine.Exceptions;
  33. namespace ZebraFlickr
  34. {
  35. internal class Program
  36. {
  37. private static void Main(string[] args)
  38. {
  39. var startColor = Console.ForegroundColor;
  40. Console.OutputEncoding = Encoding.UTF8;
  41. Console.CursorVisible = false;
  42. Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
  43. {
  44. if (CancelHandler.Cancelled)
  45. {
  46. Log.Warn("Aborting all processes NOW");
  47. Console.ForegroundColor = startColor;
  48. Environment.Exit(1);
  49. }
  50. else
  51. {
  52. Log.Warn("Cancelling process - attempting to do it gracefully. Pressing Ctrl+C again will exit immedietly.");
  53. e.Cancel = true;
  54. CancelHandler.Cancel();
  55. }
  56. };
  57. try
  58. {
  59. IAction action = null;
  60. try
  61. {
  62. action = ActionHelper.Resolve(args);
  63. if (action == null)
  64. throw new CommandLineException("Unrecognised command line");
  65. action.Run();
  66. }
  67. catch (ValidationException ex)
  68. {
  69. var writer = Console.Error;
  70. BaseAction.PrintLogo(false, writer);
  71. writer.WriteLine("Error: {0}", ex.Message);
  72. writer.WriteLine("try 'zebraflickr help' for more information.");
  73. Environment.ExitCode = 1;
  74. }
  75. catch (Exception ex)
  76. {
  77. var writer = Console.Error;
  78. BaseAction.PrintLogo(false, writer);
  79. writer.WriteLine("Error: " + ex.Message);
  80. writer.WriteLine();
  81. //if the action didn't even get made, or the verbosity is debug, dump the stack trace out
  82. if (action == null || action.Verbosity >= LogLevel.Debug)
  83. {
  84. writer.WriteLine("Full error: " + ex);
  85. }
  86. Environment.ExitCode = 1;
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.Error.WriteLine(ex.ToString());
  92. Environment.ExitCode = 1;
  93. }
  94. finally
  95. {
  96. Console.ForegroundColor = startColor;
  97. if (Debugger.IsAttached)
  98. Console.ReadLine();
  99. }
  100. }
  101. }
  102. }