Example #1
0
        /// <inheritdoc />
        public bool ExecuteCommand(ICommandEngineContext context, string commandLine)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(commandLine))
            {
                return(true); // just ignore empty command, it's fine to do nothing ;-)
                // TODO: add option in configuration to rather display help-nagging screen instead - while this is fine for running engine, better do something on
            }

            string[] commandLineArguments = CommandLineUtilities.SplitCommandLine(commandLine).ToArray();

            return(this.ExecuteCommand(context, commandLineArguments));
        }
        public void Run(ICommandEngineContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            while (!context.ShallTerminate)
            {
                this.DisplayPrompt(context.GetCurrentPrompt()); // TODO: perhaps multi-color prompt support?

                string cmdString = this._console.ReadLine();
                if (string.IsNullOrWhiteSpace(cmdString))
                {
                    continue;
                }

                this.ExecuteCommand(context, cmdString);
            }
        }
        /// <inheritdoc />
        public bool ExecuteCommand(ICommandEngineContext context, string commandLine)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(commandLine))
            {
                return(true); // just ignore empty command, it's fine to do nothing ;-)
            }

            string[] commandLineArguments = CommandLineUtilities.SplitCommandLine(commandLine).ToArray();

            // ...
            string cmdName = commandLineArguments[0];

            if (this._helpPrinter.IsGlobalHelpRequested(cmdName))
            {
                // Global, or command help?
                if (commandLineArguments.Length > 1)
                {
                    cmdName = commandLineArguments[1];
                    CommandInfo command = this._commandManager.FindCommand(cmdName);
                    if (command != null)
                    {
                        this.PrintCommandHelp(command, commandLineArguments.Skip(2)); // pass all remaining options only for detail-full syntax help (if available / implemented)
                        return(true);
                    }
                    else
                    {
                        this._console.WriteLine(this._styles.Warning, $"Unknown command => \"{cmdName}\".");
                    }
                }

                this.PrintGlobalHelp(commandLineArguments.Skip(1));
                return(true);
            }

            CommandInfo cmd = this._commandManager.FindCommand(cmdName);

            if (cmd == null)
            {
                this._console.WriteLine(this._styles.Warning, $"Unknown command => \"{cmdName}\".");
                this._helpPrinter.PrintHelpOnHelp();
                return(false);
            }

            if (commandLineArguments.Length > 1)
            {
                // any help switch inside command line will cause displaying help instead of parsing it
                if (this._helpPrinter.IsCommandHelpRequested(commandLineArguments))
                {
                    this.PrintCommandHelp(cmd, commandLineArguments.Skip(2)); // pass all remaining options only for detail-full syntax help (if available / implemented)
                    return(true);
                }
            }

            // This might crash-throw if invalid types defined. Fine.
            var model         = this.BuildModelForCommand(cmd, commandLineArguments.Skip(1));
            var outputManager = new OutputManager(this._console, this._styles, this._options.UiCulture);

            if (model == null)
            {
                // bad syntax
                return(false);
            }

            try
            {
                cmd.Command.Execute(context, outputManager, model); // skip only cmdName itself
            }
            catch (Exception ex)
            {
                this._console.WriteLine(this._styles.Error, "An exception occurred during command execution:");
                this._console.WriteLine(this._styles.Error, ex.ToString());

                // TODO: log also to file?
                return(false);
            }

            return(true);
        }
Example #4
0
        public bool ExecuteCommand(ICommandEngineContext context, string[] commandLineArguments)
        {
            if (commandLineArguments == null || !commandLineArguments.Any())
            {
                // co commands - suggest assistance
                this._helpPrinter.PrintHelpOnHelp();
                return(false);
            }

            // ...
            string cmdName = commandLineArguments[0];

            if (this._helpPrinter.IsGlobalHelpRequested(cmdName))
            {
                // Global, or command help?
                if (commandLineArguments.Length > 1)
                {
                    cmdName = commandLineArguments[1];
                    CommandInfo command = this._commandManager.FindCommand(cmdName);
                    if (command != null)
                    {
                        this.PrintCommandHelp(command, commandLineArguments.Skip(2)); // pass all remaining options only for detail-full syntax help (if available / implemented)
                        return(true);
                    }
                    else
                    {
                        this._console.WriteLine(this._styles.Warning, $"Unknown command => \"{cmdName}\".");
                    }
                }

                this.PrintGlobalHelp(commandLineArguments.Skip(1));
                return(true);
            }

            CommandInfo cmd = this._commandManager.FindCommand(cmdName);

            if (cmd == null)
            {
                this._console.WriteLine(this._styles.Warning, $"Unknown command => \"{cmdName}\".");
                this._helpPrinter.PrintHelpOnHelp();
                return(false);
            }

            if (commandLineArguments.Length > 1)
            {
                // any help switch inside command line will cause displaying help instead of parsing it
                if (this._helpPrinter.IsCommandHelpRequested(commandLineArguments))
                {
                    this.PrintCommandHelp(cmd, commandLineArguments.Skip(2)); // pass all remaining options only for detail-full syntax help (if available / implemented)
                    return(true);
                }
            }

            // This might crash-throw if invalid types defined. Fine.
            var model = this.BuildModelForCommand(cmd, commandLineArguments.Skip(1));

            if (model == null)
            {
                // bad syntax
                return(false);
            }

            try
            {
                try
                {
                    cmd.Command.Execute(context, _outputManager, model); // skip only cmdName itself
                }
                catch (Exception ex)
                {
                    this._console.WriteLine(this._styles.Error, "An exception occurred during command execution:");
                    this._console.WriteLine(this._styles.Error, ex.ToString());

                    // TODO: log also to file?
                    return(false);
                }
            }
            finally
            {
                // reset color to default after each command - best for CMD execution
                this._console.SetColors(this._styles.Default);
            }

            return(true);
        }