StartupConfiguration.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public bool IsConsoleDebug {
  81. get
  82. {
  83. return _isConsoleDebug;
  84. }
  85. set
  86. {
  87. _isConsoleDebug = value;
  88. DebugConsoleWriter.IsWriteDebugEnabled = value;
  89. }
  90. }
  91. #endregion
  92. #region *** Constructor ***
  93. public StartupConfiguration(string consoleName, Version consoleVersion)
  94. {
  95. ConsoleName = consoleName;
  96. ConsoleVersion = consoleVersion;
  97. IsInitFromXml = false;
  98. InitDefaults();
  99. }
  100. public StartupConfiguration(string consoleXmlFileName)
  101. {
  102. IsInitFromXml = true;
  103. InitDefaults();
  104. }
  105. public StartupConfiguration(XPathNavigator configurationNavigator)
  106. {
  107. IsInitFromXml = true;
  108. InitDefaults();
  109. }
  110. #endregion
  111. #region *** Public Operations ***
  112. public StartupConfiguration EnableDebugMode()
  113. {
  114. IsConsoleDebug = true;
  115. return this;
  116. }
  117. public StartupConfiguration DisableDebugMode()
  118. {
  119. IsConsoleDebug = false;
  120. return this;
  121. }
  122. public StartupConfiguration DisableDefaultCommands()
  123. {
  124. return DisableCommands(_defaultCommands);
  125. }
  126. public StartupConfiguration EnableDefaultCommands()
  127. {
  128. return EnableCommands(_defaultCommands);
  129. }
  130. public StartupConfiguration EnableCommands(params string[] commandClassNames)
  131. {
  132. if (commandClassNames == null)
  133. return this;
  134. foreach (var className in commandClassNames)
  135. {
  136. if (_disabledCommands.Contains(className))
  137. _disabledCommands.Remove(className);
  138. }
  139. return this;
  140. }
  141. public StartupConfiguration DisableCommands(params string[] commandClassNames)
  142. {
  143. if (commandClassNames == null)
  144. return this;
  145. foreach (var className in commandClassNames)
  146. {
  147. if (!_disabledCommands.Contains(className))
  148. _disabledCommands.Add(className);
  149. }
  150. return this;
  151. }
  152. #endregion
  153. #region *** Private Operations ***
  154. private void InitDefaults()
  155. {
  156. CharacterLineIntroduce = Defaults.RoleCharacters.ConsoleLineIntroduce;
  157. CharacterOrdinalArgumentSeparator = Defaults.RoleCharacters.OrdinalArgumentSeparator;
  158. CharacterNamedArgumentSeparator = Defaults.RoleCharacters.NamedArgumentSeparator;
  159. CharacterNamedArgumentValueSeparator = Defaults.RoleCharacters.NamedArgumentValueSeparator;
  160. CharacterTextValueBraceSeparator = Defaults.RoleCharacters.TextValueBraceSeparator;
  161. CharacterSelectionInputValueSeparator = Defaults.RoleCharacters.InputSelectionValueSeparator;
  162. DateFormat = Defaults.Formats.DateFormat;
  163. TimeFormat = Defaults.Formats.TimeFormat;
  164. AllowInteractive = Defaults.Console.InteractiveAllowed;
  165. AllowPreset = Defaults.Console.PresetAllowed;
  166. IsConsoleDebug = Defaults.Console.ConsoleDebugMode;
  167. WaitOnKeyInNonInteractiveMode = Defaults.Console.WaitOnKeyInNonInteractiveMode;
  168. }
  169. #endregion
  170. }
  171. }