StartupConfiguration.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Xml.XPath;
  6. using Quadarax.Foundation.Core.Console;
  7. using Quadarax.Foundation.Core.QConsole.Handlers;
  8. namespace Quadarax.Foundation.Core.QConsole.Configuration
  9. {
  10. public class StartupConfiguration
  11. {
  12. #region *** Private Fields ***
  13. private bool _isConsoleDebug;
  14. private string[] _defaultCommands = {"CmdClear", "CmdExit", "CmdPrint", "CmdSelection"};
  15. private readonly IList<string> _disabledCommands = new List<string>();
  16. #endregion
  17. #region *** Properties ***
  18. /// <summary>
  19. /// Name of console application, If not defined uses default name from entryAssembly.
  20. /// </summary>
  21. public string ConsoleName { get; }
  22. /// <summary>
  23. /// Console application version. If not defined uses version of entryAssembly.
  24. /// </summary>
  25. public Version ConsoleVersion { get; }
  26. /// <summary>
  27. /// Console application copyright message. If not set skip this info.
  28. /// </summary>
  29. public string ConsoleCopyright { get; set; }
  30. /// <summary>
  31. /// Defines if console is initialized from XML definition.
  32. /// </summary>
  33. public bool IsInitFromXml { get; }
  34. /// <summary>
  35. /// Left introducing of interactive mode character.
  36. /// </summary>
  37. public string CharacterLineIntroduce { get; set; }
  38. /// <summary>
  39. /// Separator between two ordinal arguments
  40. /// </summary>
  41. public string CharacterOrdinalArgumentSeparator { get; set; }
  42. /// <summary>
  43. /// Named argument prefix character.
  44. /// </summary>
  45. public string CharacterNamedArgumentSeparator { get; set; }
  46. /// <summary>
  47. /// Named argument postfix character {defines end of argument code and value)
  48. /// </summary>
  49. public string CharacterNamedArgumentValueSeparator { get; set; }
  50. /// <summary>
  51. /// Start end end string value character
  52. /// </summary>
  53. public string CharacterTextValueBraceSeparator { get; set; }
  54. /// <summary>
  55. /// Separator between two selection inputs
  56. /// </summary>
  57. public string CharacterSelectionInputValueSeparator { get; set; }
  58. /// <summary>
  59. /// Date input/output value format
  60. /// </summary>
  61. public string DateFormat { get; set; }
  62. /// <summary>
  63. /// Time input/output value format
  64. /// </summary>
  65. public string TimeFormat { get; set; }
  66. /// <summary>
  67. /// Defines if interactive mode is enabled
  68. /// </summary>
  69. public bool AllowInteractive { get; set; }
  70. /// <summary>
  71. /// Defines if initial variables is allowed for interactive mode.
  72. /// </summary>
  73. public bool AllowPreset { get; set; }
  74. /// <summary>
  75. /// Defines assembly list where custom commands are defined (in case explicit command definition).
  76. /// </summary>
  77. public Assembly[] CommandDefinitionAssebmly { get; set; }
  78. /// <summary>
  79. /// Returns name of command classes that will be disabled (hidden)
  80. /// </summary>
  81. public string[] DisabledCommands => _disabledCommands.ToArray();
  82. /// <summary>
  83. /// Defines if waits on key at the end of process in non-interactive mode (inline mode)
  84. /// </summary>
  85. public bool WaitOnKeyInNonInteractiveMode { get; set; }
  86. /// <summary>
  87. /// Defines if show status of engine and command context before every command executed.
  88. /// </summary>
  89. public bool ShowStatusOnEveryCommand { get; set; }
  90. public bool UseDefaultHandlers { get; set; }
  91. /// <summary>
  92. /// Handler when no arguments input specified. If not set then do nothing.
  93. /// </summary>
  94. public NothingToDo HandlerNothingToDo { get; set; }
  95. public bool IsConsoleDebug {
  96. get
  97. {
  98. return _isConsoleDebug;
  99. }
  100. set
  101. {
  102. _isConsoleDebug = value;
  103. DebugConsoleWriter.IsWriteDebugEnabled = value;
  104. }
  105. }
  106. #endregion
  107. #region *** Constructor ***
  108. public StartupConfiguration(string consoleName, Version consoleVersion)
  109. {
  110. ConsoleName = consoleName;
  111. ConsoleVersion = consoleVersion;
  112. IsInitFromXml = false;
  113. InitDefaults();
  114. }
  115. public StartupConfiguration(string consoleXmlFileName)
  116. {
  117. IsInitFromXml = true;
  118. InitDefaults();
  119. }
  120. public StartupConfiguration(XPathNavigator configurationNavigator)
  121. {
  122. IsInitFromXml = true;
  123. InitDefaults();
  124. }
  125. #endregion
  126. #region *** Public Operations ***
  127. public StartupConfiguration EnableDebugMode()
  128. {
  129. IsConsoleDebug = true;
  130. return this;
  131. }
  132. public StartupConfiguration DisableDebugMode()
  133. {
  134. IsConsoleDebug = false;
  135. return this;
  136. }
  137. public StartupConfiguration DisableDefaultCommands()
  138. {
  139. return DisableCommands(_defaultCommands);
  140. }
  141. public StartupConfiguration EnableDefaultCommands()
  142. {
  143. return EnableCommands(_defaultCommands);
  144. }
  145. public StartupConfiguration EnableCommands(params string[] commandClassNames)
  146. {
  147. if (commandClassNames == null)
  148. return this;
  149. foreach (var className in commandClassNames)
  150. {
  151. if (_disabledCommands.Contains(className))
  152. _disabledCommands.Remove(className);
  153. }
  154. return this;
  155. }
  156. public StartupConfiguration DisableCommands(params string[] commandClassNames)
  157. {
  158. if (commandClassNames == null)
  159. return this;
  160. foreach (var className in commandClassNames)
  161. {
  162. if (!_disabledCommands.Contains(className))
  163. _disabledCommands.Add(className);
  164. }
  165. return this;
  166. }
  167. #endregion
  168. #region *** Private Operations ***
  169. private void InitDefaults()
  170. {
  171. CharacterLineIntroduce = Defaults.RoleCharacters.ConsoleLineIntroduce;
  172. CharacterOrdinalArgumentSeparator = Defaults.RoleCharacters.OrdinalArgumentSeparator;
  173. CharacterNamedArgumentSeparator = Defaults.RoleCharacters.NamedArgumentSeparator;
  174. CharacterNamedArgumentValueSeparator = Defaults.RoleCharacters.NamedArgumentValueSeparator;
  175. CharacterTextValueBraceSeparator = Defaults.RoleCharacters.TextValueBraceSeparator;
  176. CharacterSelectionInputValueSeparator = Defaults.RoleCharacters.InputSelectionValueSeparator;
  177. DateFormat = Defaults.Formats.DateFormat;
  178. TimeFormat = Defaults.Formats.TimeFormat;
  179. AllowInteractive = Defaults.Console.InteractiveAllowed;
  180. AllowPreset = Defaults.Console.PresetAllowed;
  181. IsConsoleDebug = Defaults.Console.ConsoleDebugMode;
  182. WaitOnKeyInNonInteractiveMode = Defaults.Console.WaitOnKeyInNonInteractiveMode;
  183. ShowStatusOnEveryCommand = Defaults.Console.ShowStatusOnEveryCommand;
  184. UseDefaultHandlers = Defaults.Console.UseDefaultHadlers;
  185. if (UseDefaultHandlers)
  186. {
  187. HandlerNothingToDo += engine =>
  188. {
  189. using (var writer = new ConsoleWriter(ConsoleColor.Yellow))
  190. {
  191. writer.WriteLine("Nothing to do.");
  192. }
  193. using (var writer = new ConsoleWriter(ConsoleColor.DarkGreen))
  194. {
  195. writer.WriteLine("Put some arguments to do something. For example -help to list commands and arguments.");
  196. }
  197. };
  198. }
  199. }
  200. #endregion
  201. }
  202. }