using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Xml.XPath; using Quadarax.Foundation.Core.Console; using Quadarax.Foundation.Core.Logging; using Quadarax.Foundation.Core.QConsole.Handlers; namespace Quadarax.Foundation.Core.QConsole.Configuration { public class StartupConfiguration { #region *** Private Fields *** private bool _isConsoleDebug; private string[] _defaultCommands = {"CmdClear", "CmdExit", "CmdPrint", "CmdSelection"}; private readonly IList _disabledCommands = new List(); #endregion #region *** Properties *** /// /// Name of console application, If not defined uses default name from entryAssembly. /// public string ConsoleName { get; } = string.Empty; /// /// Console application version. If not defined uses version of entryAssembly. /// public Version ConsoleVersion { get; } = Version.Parse("0.0.0.0"); /// /// Console application copyright message. If not set skip this info. /// public string ConsoleCopyright { get; set; } = string.Empty; /// /// Defines if console is initialized from XML definition. /// public bool IsInitFromXml { get; } /// /// Left introducing of interactive mode character. /// public string CharacterLineIntroduce { get; set; } = string.Empty; /// /// Separator between two ordinal arguments /// public string CharacterOrdinalArgumentSeparator { get; set; } = string.Empty; /// /// Named argument prefix character. /// public string CharacterNamedArgumentSeparator { get; set; } = string.Empty; /// /// Named argument postfix character {defines end of argument code and value) /// public string CharacterNamedArgumentValueSeparator { get; set; } = string.Empty; /// /// Start end end string value character /// public string CharacterTextValueBraceSeparator { get; set; } = string.Empty; /// /// Separator between two selection inputs /// public string CharacterSelectionInputValueSeparator { get; set; } = string.Empty; /// /// Date input/output value format /// public string DateFormat { get; set; } = string.Empty; /// /// Time input/output value format /// public string TimeFormat { get; set; } = string.Empty; /// /// Defines if interactive mode is enabled /// public bool AllowInteractive { get; set; } /// /// Defines if initial variables is allowed for interactive mode. /// public bool AllowPreset { get; set; } /// /// Defines assembly list where custom commands are defined (in case explicit command definition). /// public Assembly[] CommandDefinitionAssembly { get; set; } = Array.Empty(); /// /// Returns name of command classes that will be disabled (hidden) /// public string[] DisabledCommands => _disabledCommands.ToArray(); /// /// Defines if waits on key at the end of process in non-interactive mode (inline mode) /// public bool WaitOnKeyInNonInteractiveMode { get; set; } /// /// Defines if show status of engine and command context before every command executed. /// public bool ShowStatusOnEveryCommand { get; set; } /// /// Defines if throws exception when command execution is not successful. /// public bool ThrowExceptionWhenNotSuccess { get; set; } = false; public bool UseDefaultHandlers { get; set; } /// /// Handler when no arguments input specified. If not set then do nothing. /// public NothingToDo? HandlerNothingToDo { get; set; } public bool IsConsoleDebug { get { return _isConsoleDebug; } set { _isConsoleDebug = value; DebugConsoleWriter.IsWriteDebugEnabled = value; } } public ILogger? DefaultLoggerFactory { get; set; } #endregion #region *** Constructor *** public StartupConfiguration(string consoleName, Version consoleVersion) { ConsoleName = consoleName; ConsoleVersion = consoleVersion; IsInitFromXml = false; InitDefaults(); } public StartupConfiguration(string consoleXmlFileName) { IsInitFromXml = true; InitDefaults(); } public StartupConfiguration(XPathNavigator configurationNavigator) { IsInitFromXml = true; InitDefaults(); } #endregion #region *** Public Operations *** public StartupConfiguration EnableDebugMode() { IsConsoleDebug = true; return this; } public StartupConfiguration DisableDebugMode() { IsConsoleDebug = false; return this; } public StartupConfiguration DisableDefaultCommands() { return DisableCommands(_defaultCommands); } public StartupConfiguration EnableDefaultCommands() { return EnableCommands(_defaultCommands); } public StartupConfiguration EnableCommands(params string[]? commandClassNames) { if (commandClassNames == null) return this; foreach (var className in commandClassNames) { if (_disabledCommands.Contains(className)) _disabledCommands.Remove(className); } return this; } public StartupConfiguration DisableCommands(params string[]? commandClassNames) { if (commandClassNames == null) return this; foreach (var className in commandClassNames) { if (!_disabledCommands.Contains(className)) _disabledCommands.Add(className); } return this; } #endregion #region *** Private Operations *** private void InitDefaults() { CharacterLineIntroduce = Defaults.RoleCharacters.ConsoleLineIntroduce; CharacterOrdinalArgumentSeparator = Defaults.RoleCharacters.OrdinalArgumentSeparator; CharacterNamedArgumentSeparator = Defaults.RoleCharacters.NamedArgumentSeparator; CharacterNamedArgumentValueSeparator = Defaults.RoleCharacters.NamedArgumentValueSeparator; CharacterTextValueBraceSeparator = Defaults.RoleCharacters.TextValueBraceSeparator; CharacterSelectionInputValueSeparator = Defaults.RoleCharacters.InputSelectionValueSeparator; DateFormat = Defaults.Formats.DateFormat; TimeFormat = Defaults.Formats.TimeFormat; AllowInteractive = Defaults.Console.InteractiveAllowed; AllowPreset = Defaults.Console.PresetAllowed; IsConsoleDebug = Defaults.Console.ConsoleDebugMode; WaitOnKeyInNonInteractiveMode = Defaults.Console.WaitOnKeyInNonInteractiveMode; ShowStatusOnEveryCommand = Defaults.Console.ShowStatusOnEveryCommand; UseDefaultHandlers = Defaults.Console.UseDefaultHadlers; if (UseDefaultHandlers) { HandlerNothingToDo += engine => { using (var writer = new ConsoleWriter(ConsoleColor.Yellow)) { writer.WriteLine("Nothing to do."); } using (var writer = new ConsoleWriter(ConsoleColor.DarkGreen)) { writer.WriteLine("Put some arguments to do something. For example -help to list commands and arguments."); } }; } } #endregion } }