private static void Execute(string userInput) { try { CParsedInput parsedInput = new CParsedInput(userInput); parsedInput.CommandMethodInfo.Invoke(null, parsedInput.Parameters); } catch (WrongCallFormatException) { CFormat.WriteLine("Wrong call format.", "The call should be as it follows: <Library>.<Command> [arg1] [arg2] [etc.]"); } catch (LibraryNotExistingException) { CFormat.WriteLine("This library does not exist."); } catch (CommandNotExistingException) { CFormat.WriteLine("This command does not exist."); } catch (MissingArgumentException ex) { CFormat.WriteLine("Missing required argument.", CFormat.GetArgsFormat(ex.ParsedInput.FullCallName, ex.ParsedInput.CommandMethodInfo.GetParameters())); } catch (ArgumentCoerceException ex) { CFormat.WriteLine(string.Format("The argument '{0}' cannot be parsed to type '{1}'", ex.ArgumentName, ex.ArgumentTargetType)); } catch (Exception ex) { CFormat.WriteLine(ex.Message); } }
public static object ReadFromConsole( string promptMessage = "", ConsoleInputType inputType = ConsoleInputType.String, bool canEscape = false, int maxChars = -1, char charMask = '\0') { ConsoleColor foregroundColor = Console.ForegroundColor; bool maskFlag = charMask > char.MinValue; bool runFlag = true; promptMessage = promptMessage == "" ? "MMaster> " : promptMessage; cursorPos = 0; historyIndex = 0; tabAllowed = true; _readBuffer = ""; Console.ForegroundColor = ConsoleColor.White; Console.Write(promptMessage); while (runFlag) { ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true); // READ BUFFER if (consoleKeyInfo.Key == ConsoleKey.Enter) { ValidateHistory(); if (!String.IsNullOrEmpty(_readBuffer)) { AddToHistory(_readBuffer); } Console.ForegroundColor = foregroundColor; CFormat.JumpLine(); switch (inputType) { case ConsoleInputType.String: return(_readBuffer); case ConsoleInputType.Int: if (String.IsNullOrEmpty(_readBuffer)) { return(null); } return(int.Parse(_readBuffer)); case ConsoleInputType.Double: if (String.IsNullOrEmpty(_readBuffer)) { return(null); } return(double.Parse(_readBuffer.Replace(".", ","))); } } else if (consoleKeyInfo.Key == ConsoleKey.Backspace) { ValidateHistory(); if (cursorPos > 0) { if (cursorPos != _readBuffer.Length) { string str2 = _readBuffer.Substring(0, cursorPos - 1); string str3 = _readBuffer.Substring(cursorPos, _readBuffer.Length - cursorPos); _readBuffer = str2 + str3; MoveCursorBack(); UserWrite(str3 + " "); for (int index = 0; index < str3.Length + 1; ++index) { MoveCursorBack(); } } else { _readBuffer = _readBuffer.Substring(0, _readBuffer.Length - 1); MoveCursorBack(); UserWrite(" "); MoveCursorBack(); } } } else if (consoleKeyInfo.Key == ConsoleKey.Delete) { ValidateHistory(); if (cursorPos < _readBuffer.Length) { string str2 = _readBuffer.Substring(0, cursorPos); string str3 = _readBuffer.Substring(cursorPos + 1, _readBuffer.Length - cursorPos - 1); _readBuffer = str2 + str3; UserWrite(str3 + " "); for (int index = 0; index < str3.Length + 1; ++index) { MoveCursorBack(); } } } else if (consoleKeyInfo.Key == ConsoleKey.Home) { for (int cursorPos = CInput.cursorPos; cursorPos > 0; --cursorPos) { MoveCursorBack(); } } else if (consoleKeyInfo.Key == ConsoleKey.End) { for (int cursorPos = CInput.cursorPos; cursorPos < _readBuffer.Length; ++cursorPos) { MoveCursorAhead(); } } else if (consoleKeyInfo.Key == ConsoleKey.Escape) { if (canEscape) { return(null); } } else if (consoleKeyInfo.Key == ConsoleKey.Insert) { insertMode = !insertMode; Console.CursorSize = !insertMode ? 1 : 100; } else if (consoleKeyInfo.Key != ConsoleKey.Spacebar && consoleKeyInfo.KeyChar == char.MinValue) { if (consoleKeyInfo.Key == ConsoleKey.RightArrow && cursorPos < _readBuffer.Length) { MoveCursorAhead(); } else if (consoleKeyInfo.Key == ConsoleKey.LeftArrow && cursorPos > 0) { MoveCursorBack(); } else if (consoleKeyInfo.Key == ConsoleKey.UpArrow) { OlderHistory(); } else if (consoleKeyInfo.Key == ConsoleKey.DownArrow) { NewerHistory(); } } // Tab auto-completition else if (consoleKeyInfo.Key == ConsoleKey.Tab) { if (String.IsNullOrEmpty(_readBuffer) || !tabAllowed || _readBuffer.Contains(" ")) { continue; } if (!_readBuffer.Contains(".")) // If looking for a library OR default command { //Try internal libraries first string result = CommandManager.InternalLibraryCallNames.Keys.FirstOrDefault(x => x.ToLower().StartsWith(_readBuffer.ToLower())); //Then try external libraries if (result == null) { result = CommandManager.ExternalLibraryCallNames.Keys.FirstOrDefault(x => x.ToLower().StartsWith(_readBuffer.ToLower())); } // If still null, no result at all in libraries if (result != null) { for (int i = 0; i < _readBuffer.Length; i++) { MoveCursorBack(); } UserWrite(result); _readBuffer = result; } // Then trying in Default library, the only one that can be called without library. result = CommandManager.InternalLibraries[typeof(Default)].Keys.FirstOrDefault(x => x.ToLower().StartsWith(_readBuffer.ToLower())); if (result == null) { continue; } else { for (int i = 0; i < _readBuffer.Length; i++) { MoveCursorBack(); } UserWrite(result); _readBuffer = result; } } else // If the buffer contains a point. { // PARSE LIBRARY Type library = CParsedInput.ParseLibrary(_readBuffer.Split('.')[0]); string commandInputLower = _readBuffer.Split('.')[1].ToLower(); if (library == null || commandInputLower == "") { continue; } // Try internal libraries string result = null; if (CommandManager.InternalLibraries.ContainsKey(library)) { result = CommandManager.InternalLibraries[library].Keys.FirstOrDefault(x => x.ToLower().StartsWith(commandInputLower)); } // Then try external else { result = CommandManager.ExternalLibraries[library].Keys.FirstOrDefault(x => x.ToLower().StartsWith(commandInputLower)); } // If result found if (result != null) { for (int i = 0; i < _readBuffer.Length; i++) { MoveCursorBack(); } string libraryCallName = library.GetCustomAttribute <MMasterLibrary>().CallName; UserWrite(libraryCallName + "." + result); _readBuffer = libraryCallName + "." + result; } } continue; } else { CInput.ValidateHistory(); if (maxChars <= 0 || CInput._readBuffer.Length < maxChars) { char keyChar; switch (inputType) { case ConsoleInputType.Int: keyChar = consoleKeyInfo.KeyChar; int num1; if (!int.TryParse(keyChar.ToString(), out _)) { keyChar = consoleKeyInfo.KeyChar; if (!(keyChar.ToString() != "-")) { keyChar = consoleKeyInfo.KeyChar; num1 = !(keyChar.ToString() == "-") ? 0 : ((uint)CInput._readBuffer.Length > 0U ? 1 : 0); } else { num1 = 1; } } else { num1 = 0; } if (num1 == 0) { break; } continue; case ConsoleInputType.Double: keyChar = consoleKeyInfo.KeyChar; int num2; if (!int.TryParse(keyChar.ToString(), out _)) { keyChar = consoleKeyInfo.KeyChar; if (!(keyChar.ToString() != ".")) { keyChar = consoleKeyInfo.KeyChar; if (!(keyChar.ToString() == ".") || !CInput._readBuffer.Contains(".")) { goto label_54; } } keyChar = consoleKeyInfo.KeyChar; if (!(keyChar.ToString() != "-")) { keyChar = consoleKeyInfo.KeyChar; num2 = !(keyChar.ToString() == "-") ? 0 : (CInput._readBuffer.Length != 0 ? 1 : (CInput._readBuffer.Contains(".") ? 1 : 0)); goto label_55; } else { num2 = 1; goto label_55; } } label_54: num2 = 0; label_55: if (num2 == 0) { break; } continue; } if (CInput.cursorPos != CInput._readBuffer.Length && !CInput.insertMode) // If in the word, insert mode off { string str2 = CInput._readBuffer.Substring(0, CInput.cursorPos); keyChar = consoleKeyInfo.KeyChar; string str3 = keyChar.ToString(); string str4 = CInput._readBuffer.Substring(CInput.cursorPos, CInput._readBuffer.Length - CInput.cursorPos); CInput._readBuffer = str2 + str3 + str4; } else if (CInput.cursorPos != CInput._readBuffer.Length && CInput.insertMode) // If in the word, insert mode on { string str2 = CInput._readBuffer.Substring(0, CInput.cursorPos); keyChar = consoleKeyInfo.KeyChar; string str3 = keyChar.ToString(); string str4 = CInput._readBuffer.Substring(CInput.cursorPos + 1, CInput._readBuffer.Length - CInput.cursorPos - 1); CInput._readBuffer = str2 + str3 + str4; } else { string readBuffer = CInput._readBuffer; keyChar = consoleKeyInfo.KeyChar; string str2 = keyChar.ToString(); CInput._readBuffer = readBuffer + str2; } // PRINT TO SCREEN if (maskFlag) { CInput.UserWrite(charMask.ToString()); } else { keyChar = consoleKeyInfo.KeyChar; CInput.UserWrite(keyChar.ToString()); if (CInput.cursorPos != CInput._readBuffer.Length && !CInput.insertMode) { string s = CInput._readBuffer.Substring(CInput.cursorPos, CInput._readBuffer.Length - CInput.cursorPos); CInput.UserWrite(s); for (int index = 0; index < s.Length; ++index) { CInput.MoveCursorBack(); } } } } } } return(null); }