예제 #1
0
        /// <summary>
        /// Runs a command session (by letting the user enter a series
        /// of commands without re-starting the application).
        /// </summary>
        /// <param name="session">The session controller</param>
        static void RunSession(AltCmdSession session)
        {
            ExecutionContext ec = session.Context;

            for (; ;)
            {
                Console.Write($"{ec}> ");
                string cmd = Console.ReadLine();
                if (cmd.StartsWithIgnoreCase("ex") || cmd.StartsWithIgnoreCase("q"))
                {
                    return;
                }

                try
                {
                    if (cmd.StartsWith("@"))
                    {
                        ProcessCmdFile(session, cmd.Substring(1));
                    }
                    else
                    {
                        session.Execute(cmd);
                    }
                }

                catch (Exception ex)
                {
                    ShowError(ex);
                }
            }
        }
예제 #2
0
        public static void Main(string[] args)
        {
            try
            {
                // Establish the execution context
                var ec = GetContext(args);

                // Register any command processors
                //ec.Processors.Add(new NameProcessor());

                // TODO: There should likely be a processor that handles the command
                // model itself (which would always get processed last)...

                // Initialize model(s) by loading commands related to the
                // current branch (plus branches that have been merged into it)
                ec.InitializeModels();

                var session = new AltCmdSession(ec);

                if (ec.Store != null && args.Length <= 1)
                {
                    // Start an interactive command line session if the
                    // command store has been determined, but there are
                    // no other command line arguments.

                    RunSession(session);
                }
                else
                {
                    // Process the command line. Start an interactive session
                    // if that command line leads to a memory store.

                    session.Execute(String.Join(" ", args));

                    if (ec.Store is MemoryStore)
                    {
                        RunSession(session);
                    }
                }
            }

            catch (NotImplementedException nex)
            {
                Console.WriteLine(nex.StackTrace);
                Log.Warn("Not implemented: " + nex.StackTrace);
            }

            catch (Exception ex)
            {
                ShowError(ex);
                Console.WriteLine("Hit RETURN to close");
                Console.Read();
            }
        }
예제 #3
0
        /// <summary>
        /// Processes commands listed in a command file.
        /// </summary>
        /// <param name="session">The command session</param>
        /// <param name="fileName">The path for a text file that holds the commands
        /// to be executed (one command per line).</param>
        static void ProcessCmdFile(AltCmdSession session, string fileName)
        {
            // if the file doesn't have an extension, assume ".cmd"
            bool isExisting = File.Exists(fileName);

            if (!isExisting && String.IsNullOrEmpty(Path.GetExtension(fileName)))
            {
                fileName   = fileName + ".cmd";
                isExisting = File.Exists(fileName);
            }

            if (!isExisting)
            {
                throw new ArgumentException("No such file: " + fileName);
            }

            string[] cmds    = File.ReadAllLines(fileName);
            uint     numDone = 0;

            Log.Trace($"Processing commands read from {fileName}");

            foreach (string cmd in cmds)
            {
                // Ignore blank lines, as well as lines that start with
                // recognized comment markers
                string c = cmd.Trim();
                if (c.StartsWith("!") || c.StartsWith("--") || c.StartsWith("//"))
                {
                    continue;
                }

                try
                {
                    session.Execute(cmd);
                    numDone++;
                }

                catch
                {
                    Log.Error($"Failed to process command: {c}");
                    throw;
                }
            }

            Log.Info($"Processed {numDone} commands");
        }