Exemplo n.º 1
0
 /// <summary>
 /// 添加Cmds,此为ShellContext的扩展方法(目的是较小的改动ShellContext)
 /// </summary>
 public static void AddCmds(this ShellContext context)
 {
     ShellContext.CmdManager manager = context.cmdManager;
     manager.Add(new ToJsonCmd(context));
     manager.Add(new HelpCmd(context));
     manager.Add(new NowCmd(context));
     manager.Add(new VarCmd(context));
     manager.Add(new AddCmd(context));
     manager.Add(new EchoCmd(context));
     manager.Add(new IdCmd(context));
     manager.Add(new TestCmd(context));
     manager.Add(new ScriptCmd(context));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 运行,这个函数的返回值将作为主函数的返回值
        /// </summary>
        /// <returns></returns>
        public int Run()
        {
            // Wagsn Shell 的应用上下文初始化
            ShellContext AppContext = new ShellContext();

            // 打印版本信息
            Console.Write($"{AppContext.HelloInfo}\r\n\r\nWS {AppContext.CurrentDirectory}> ");

            // 一些中间临时变量
            string nextLine = null;

            string[] nextWords = null;
            // 命令
            string cmd = null;
            // 参数:可能是多个,交由处理函数自行切割
            string arg = null;

            // 一个简单的控制台循环
            while (true)
            {
                nextLine  = Console.ReadLine().Trim();
                nextWords = nextLine.Split(' ');
                cmd       = nextWords[0];
                arg       = nextLine.Substring(cmd.Length).Trim();
                try
                {
                    switch (cmd)
                    {
                    case "":
                        break;

                    case "exit":
                        return(0);

                    case "clearlast":
                        Console.SetCursorPosition(0, Console.CursorSize - 1);
                        break;

                    case "test_split":
                        Console.WriteLine("String.Split \"hello world\" with \" \": " + JsonUtil.ToJson("hello world".Split(' '))); // ["hello","world"]
                        Console.WriteLine("String.Split \"\" with \" \": " + JsonUtil.ToJson("".Split(' ')));                       // [""]
                        break;

                    case "exception":
                        throw new Exception("test exception");

                    default:
                        AppContext.cmdManager.Run(cmd, arg);
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"命令执行失败:\r\n{e}");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine();
                }
                Console.Write($"WS {AppContext.CurrentDirectory}> ");
            }
        }