internal int Run() { InteractivePrompt.Run( (string commandLine, List <char> commandLineCharacters, List <string> commands) => { return(ExecuteCommand(commandLine)); }, PromptString, ConsoleAppResources.StartupMessage, s_completionList); return(Program.Success); }
public static bool Interactive() { GlobalVars.CounterOfEnterInteractiveMode += 1; //每次进入交互模式计数器加一 var prompt = GlobalVars.InteractivePromptString + ">"; var startupMsg = "Now will enter interactive mode......\n Tips: Type commands like linux shell (Tab to auto complete | Ctrl + C to quit) then press enter to write to device \n or Press Enter to read response"; if (GlobalVars.CounterOfEnterInteractiveMode > 1) //如果交互模式计数器大于1,不要打印提示消息 { startupMsg = String.Empty; } List <string> completionList = new List <string> { "*IDN?", "*RST", "*TST?", "*CLS" }; InteractivePrompt.Run( ((strCmd, promptt, listCmd) => { if (GlobalVars.VISA_CLI_Option_isClearConsole) { Console.Clear(); } //return strCmd.Length.ToString() + Environment.NewLine; //var handleInput = "(((--> " + strCmd + " <--)))"; //return handleInput + Environment.NewLine; if (!String.IsNullOrWhiteSpace(strCmd)) { GlobalVars.VISA_CLI_Option_CommandString = strCmd; Write(); return("cmdStr is " + GlobalVars.VISA_CLI_Option_CommandString + " Write" + Environment.NewLine); } else { GlobalVars.VISA_CLI_Option_CommandString = String.Empty; //如果输入为空,则将GlobalVars.VISA_CLI_Option_CommandString置为空,防止在异常发生后尝试再次进入交互模式时判断出现错误 while (GlobalVars.VISA_CLI_Option_isInteractiveMode || String.IsNullOrEmpty(GlobalVars.VISA_CLI_Option_CommandString)) Read(); return("cmdStr is empty,Read()" + Environment.NewLine + "ReadBack buffer is:" + Environment.NewLine + ((GlobalVars.VISA_CLI_Option_isOutputModeHex) ? (DRDigit.Fast.ToHexString(GlobalVars.VISA_CLI_ReadBackBuffer) + "\n") : (System.Text.Encoding.GetEncoding("iso-8859-1").GetString(GlobalVars.VISA_CLI_ReadBackBuffer).TrimEnd('\0')))); } }), prompt, startupMsg, completionList); return(true); }
public static void Run(string[] args, ConfigFile configFile) { try { string dbname = (args.Length > 1) && (args[1] != null) ? args[1] : null; SqliteDb db = new SqliteDb(dbname, true, null, configFile.Extract.Delimiter, configFile.Db.LogFile).Init(); if (dbname == null) { dbname = "a transient in-memory database."; } Writer = new PlainTextResultWriter(Console.Out, Console.Out, Console.Out); var tables = db.GetTableNames(); var columns = new List <string>(); foreach (var t in tables) { foreach (var col in db.GetColumnNamesForTable(t)) { if (!columns.Contains(col)) { columns.Add(col); } } } var startupMsg = "ETLyte v. " + Globals.Version; startupMsg += Environment.NewLine + "Connected to " + dbname; string prompt = "ETLyte> "; CSharpEvaluator eval = new CSharpEvaluator(); InteractivePrompt.Run( ((cmd, rawinput, completions) => { // add new completions foreach (var c in cmd.Split(' ')) { if (!completions.Contains(c)) { completions.Add(c); } } string retstr = ""; var arg = cmd.Split(' '); var firstArg = arg[0]; if (firstArg.Trim()[0] == '.') { switch (firstArg.ToLower()) { case ".tables": var sqlcmd = "SELECT name FROM sqlite_master WHERE type='table';"; if (db.ExecuteQuery(sqlcmd, Writer) != 0) { Writer.WriteStd(db.LastError); } break; case ".output": if (arg[1].ToLower() == "json") { Writer = new JsonResultWriter(Console.Out, Console.Out, Console.Out); retstr = "Switched to JSON output" + Environment.NewLine; } else if (arg[1].ToLower() == "plain") { Writer = new PlainTextResultWriter(Console.Out, Console.Out, Console.Out); retstr = "Switched to plain output" + Environment.NewLine; } break; default: retstr = "Unrecognized command." + Environment.NewLine; break; } } else if (firstArg.Trim()[0] == '{') { retstr = eval.HandleCmd(cmd.Trim().Substring(1)) + Environment.NewLine; } else { if (db.ExecuteQuery(cmd, Writer) != 0) { Writer.WriteStd(db.LastError); } } return(retstr); }), prompt, startupMsg, tables.Concat(columns).ToList()); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.InnerException); Console.WriteLine(e.StackTrace); } }
public static int RunOptionsAndReturnExitCode(Options opts) { var program = new Stack(); var code = new Stack(); program.Push(code); Interpreter interpreter; switch (opts.interpreter) { case "nonstrict": interpreter = new NonstrictInterpreter(); break; case "strict": interpreter = new StrictInterpreter(); break; default: throw new Exception($"No such interpreter '{opts.interpreter}'."); } var prompt = "> "; var trace = true; Console.WriteLine($"got a program option '{opts.program}'"); if (opts.program != null) { program = opts.program.ToStack(); if (!trace) { program = interpreter.Run(program); Console.WriteLine(program.ToRepr()); } else { Console.WriteLine(program.ToRepr()); // sb.Append(Environment.NewLine); foreach (var stack in interpreter.EvalStream(program)) { Console.WriteLine(stack.ToRepr()); // sb.Append(Environment.NewLine); program = stack; } } return(0); } var startupMsg = "Welcome to push-forth!"; List <string> completionList = interpreter.instructions.Keys.ToList(); InteractivePrompt .Run( ((strCmd, listCmd, listStrings) => { if (!opts.completeStack) { var newCode = $"[{strCmd}]".ToStack(); program.Pop(); program.Push(newCode); } else { program = strCmd.ToStack(); } string output; if (!trace) { program = interpreter.Run(program); output = program.ToRepr(); } else { var sb = new StringBuilder(); sb.Append(program.ToRepr()); sb.Append(Environment.NewLine); foreach (var stack in interpreter.EvalStream(program)) { sb.Append(stack.ToRepr()); sb.Append(Environment.NewLine); program = stack; } output = sb.ToString(); } return(strCmd + Environment.NewLine + output + Environment.NewLine); }), prompt, startupMsg, completionList); return(0); }