StartupConfiguration.cs 6.8 KB

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