Пример #1
0
        public int Execute()
        {
            if (_commandNameToCommand.Count == 0)
            {
                WriteLine(Strings.Error_NoCommands, T.ErrorTextColor);
                return(1);
            }

            var args = new ConsoleArguments();

            if (args.HasOnlyHelpSwitch() || args.CommandName == null)
            {
                IHelpGenerator help = new ConsoleHelpGenerator();
                help.GenerateHelp(this);
                return(0);
            }

            if (!_commandNameToCommand.TryGetValue(args.CommandName, out Command command))
            {
                WriteLine(string.Format(Strings.Error_UnknownCommand, args.CommandName), T.ErrorTextColor);
                return(1);
            }

            return(command.Execute(args.WithoutCommand(), _onBeforeExecuteCommand));
        }
Пример #2
0
        internal int Execute(ConsoleArguments arguments, Action <Command> onBeforeExecuteCommand)
        {
            //make sure options have a command attached
            foreach (LinePrimitive opt in _options)
            {
                opt.Command = this;
            }

            if (arguments.HasOnlyHelpSwitch())
            {
                IHelpGenerator help = new ConsoleHelpGenerator();
                help.GenerateHelp(this);
                return(0);
            }

            _args = arguments;

            onBeforeExecuteCommand?.Invoke(this);

            try
            {
                if (_onExecuteMethod != null)
                {
                    _onExecuteMethod();
                }
                else if (_onExecuteAsyncMethod != null)
                {
                    _onExecuteAsyncMethod().Wait();
                }
                else
                {
                    WriteLine(Strings.Error_CommandHasNoExecute, T.ErrorTextColor);
                    return(1);
                }

                return(0);
            }
            catch (ArgValidationException ex)
            {
                Write(ex.ParamName, T.ErrorTextColor);
                Write(": ");
                WriteLine(ex.OriginalMessage);

                return(1);
            }
            catch (Exception ex)
            {
                bool continueErrorHandling = _app.RaiseError(this, ex);

                if (continueErrorHandling)
                {
                    WriteLine(ex.Message, T.ErrorTextColor);
                }

                return(1);
            }
        }