示例#1
0
        private async Task ParseCommandsAsync(IList <Exception> errors, CommandParserResult result, CancellationToken cancellationToken)
        {
            foreach (var cmd in m_commands)
            {
                try
                {
                    await ParseCommandAsync(cmd, result, cancellationToken);

                    if (result.HelpRequested)
                    {
                        break;
                    }
                }
                catch (CommandNotFoundException e)
                {
                    logger.LogDebug("Command '{Name}' not found", cmd.Name);

                    errors.Add(e);
                }
                catch (CommandParseException e)
                {
                    logger.LogDebug("Unable to parse command '{Name}'", cmd.Name);

                    errors.Add(e);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Unknown error occured while parsing the commands");

                    errors.Add(ex);
                }
            }
        }
示例#2
0
        private async Task ValidateAsync <T>(T @object, CommandParserResult parserResult, List <Exception> errors, CancellationToken token)
        {
            if (parserResult.HelpRequested)
            {
                return;
            }

            if (!m_validators.HasValidatorFor <T>())
            {
                logger.LogDebug("No validator configured for {name} in command '{cmdName}'", typeof(T).Name, Name);

                return;
            }

            var results = (await Task.WhenAll(m_validators.GetValidators <T>()
                                              .Select(async validator => await validator.ValidateAsync(@object, token))))
                          .ToArray();

            foreach (var result in results)
            {
                if (result.IsValid)
                {
                    continue;
                }

                logger.LogDebug("Validation failed with '{message}'", result.Error.Message);

                errors.Add(result.Error);
            }
        }
示例#3
0
        private async Task ParseCommandAsync(CommandLineCommandBase cmd, CommandParserResult result, CancellationToken cancellationToken)
        {
            var found = argumentManager.TryGetValue(cmd, out var model);

            if (!found)
            {
                result.MergeResult(new CommandNotFoundParserResult(cmd));

                if (cmd.IsRequired)
                {
                    throw new CommandNotFoundException(cmd);
                }

                return;
            }
            else if (found && HelpRequested(result, cmd, model))
            {
                return;
            }

            var cmdParseResult = await cmd.ParseAsync(cancellationToken);

            if (cmdParseResult.HelpRequested)
            {
                return;
            }

            result.MergeResult(cmdParseResult);

            if (cmdParseResult.HasErrors)
            {
                throw new CommandParseException(cmd, cmdParseResult.Errors);
            }
        }
        private bool HelpRequested(CommandParserResult result, CommandLineOptionBase option, ArgumentModel model)
        {
            if (!m_parserOptions.EnableHelpOption)
            {
                return(false);
            }

            if (model.Key.Equals(m_helpOptionName, StringComparison.InvariantCultureIgnoreCase) ||
                model.Key.Equals(m_helpOptionNameLong, StringComparison.InvariantCultureIgnoreCase))
            {
                result.HelpRequestedFor = this;

                return(true);
            }
            else if (model.HasValue &&
                     (model.Value.Equals(m_helpOptionName, StringComparison.InvariantCultureIgnoreCase) ||
                      model.Value.Equals(m_helpOptionNameLong, StringComparison.InvariantCultureIgnoreCase)))
            {
                result.HelpRequestedFor = option;

                return(true);
            }

            return(false);
        }
示例#5
0
        private void ParseOption(CommandLineOptionBase option, CommandParserResult result)
        {
            bool found = argumentManager.TryGetValue(option, out ArgumentModel model);

            if (found && HelpRequested(result, option, model))
            {
                logger.LogDebug("Command Option '{Name}' got help requested.", option.ToString());

                return;
            }
            else if (!found && option.CheckOptionNotFound())
            {
                throw new OptionNotFoundException(option);
            }
            else if (option.ShouldUseDefault(found, model))
            {
                logger.LogDebug("Command Option '{Name}' using default value.", option.ToString());

                option.UseDefault();

                return;
            }
            else if (found && !option.CanParse(model))
            {
                throw new OptionParseException(option, model);
            }
            else if (!found)
            {
                return;
            }

            option.Parse(model);
        }
        public override ICommandParserResult Parse(IArgumentManager argumentManager)
        {
            var result = new CommandParserResult(this);
            var errors = new List <Exception>();

            ParseCommands(errors, result, argumentManager);

            ParseOptions(errors, result, argumentManager);

            result.MergeResult(errors);

            return(result);
        }
示例#7
0
        public override async Task <ICommandParserResult> ParseAsync(CancellationToken cancellationToken)
        {
            var result = new CommandParserResult(this);
            var errors = new List <Exception>();

            await ParseCommandsAsync(errors, result, cancellationToken);

            ParseOptions(errors, result);

            await ValidateAsync(m_commandOption, result, errors, cancellationToken);

            result.MergeResult(errors);

            return(result);
        }
示例#8
0
        private void ParseOptions(IList <Exception> errors, CommandParserResult result)
        {
            if (result.HelpRequested)
            {
                return;
            }

            foreach (var optionKeyValue in m_options)
            {
                var option = optionKeyValue.Value;

                try
                {
                    ParseOption(option, result);

                    if (result.HelpRequested)
                    {
                        break;
                    }
                }
                catch (OptionParseException e)
                {
                    logger.LogDebug("Unable to parse option '{Name}' value '{Value}'", option.ToString(), e.ArgumentModel.Values.FirstOrDefault());

                    errors.Add(e);
                }
                catch (OptionNotFoundException e)
                {
                    logger.LogDebug("Command Option '{Name}' not found! Option is marked as required, with no default values configured.", option.ToString());

                    errors.Add(e);
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Command Option '{Name}' unknown error occured during parsing.", option.ToString());

                    errors.Add(e);
                }
            }
        }
示例#9
0
        private bool HelpRequested(CommandParserResult result, IArgument option, ArgumentModel model)
        {
            if (!m_parserOptions.EnableHelpOption)
            {
                return(false);
            }

            if (IsHelpOption(model.Key))
            {
                result.HelpRequestedFor = this;

                return(true);
            }
            else if (model.HasValue && ContainsHelpOption(model.Values))
            {
                result.HelpRequestedFor = option;

                return(true);
            }

            return(false);
        }
        private void ParseCommands(IList <Exception> errors, CommandParserResult result, IArgumentManager argumentManager)
        {
            foreach (var cmd in m_commands)
            {
                try
                {
                    if (!argumentManager.TryGetValue(cmd, out ArgumentModel model))
                    {
                        result.MergeResult(new CommandNotFoundParserResult(cmd));

                        if (cmd.IsRequired)
                        {
                            throw new CommandNotFoundException(cmd);
                        }

                        continue;
                    }

                    var cmdParseResult = cmd.Parse(argumentManager);

                    if (cmdParseResult.HelpRequested)
                    {
                        break;
                    }

                    result.MergeResult(cmdParseResult);

                    if (cmdParseResult.HasErrors)
                    {
                        throw new CommandParseException(cmd, cmdParseResult.Errors);
                    }
                }
                catch (Exception ex)
                {
                    errors.Add(ex);
                }
            }
        }
        private void ParseOptions(IList <Exception> errors, CommandParserResult result, IArgumentManager argumentManager)
        {
            foreach (var o in m_options)
            {
                try
                {
                    var  option = o.Value;
                    bool found  = argumentManager.TryGetValue(option, out ArgumentModel model);

                    if (found && HelpRequested(result, option, model))
                    {
                        break;
                    }
                    else if (!found && option.IsRequired && !option.HasDefault)
                    {
                        throw new OptionNotFoundException(option);
                    }
                    else if ((!found && !model.HasValue && option.HasDefault) ||
                             (found && !option.CanParse(model) && option.HasDefault))
                    {
                        option.UseDefault();

                        continue;
                    }
                    else if (found && !option.CanParse(model))
                    {
                        throw new OptionParseException(option, model);
                    }

                    option.Parse(model);
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }
        }