/// <summary>
 /// Validates provided parsed option for compliance with this option definition.
 /// </summary>
 /// <param name="parsedOption">parsed option that should be validated</param>
 /// <returns>true if the provided option is valid; otherwise returns false</returns>
 internal ValueValidationResult Validate(OptionParsed parsedOption)
 {
     if (parsedOption.Value == null)
     {
         return(ValueValidationResult.OK);
     }
     if (_valueParser == null)
     {
         throw new Exception("Internal Error");
     }
     return(_valueParser.ValidateValue(parsedOption.Value));
 }
        private static void ProcessOption(string option, Stack <string> tokens, bool isShort)
        {
            string[] arguments  = option.Split(new char[] { '=' }, 2);
            string   optionName = arguments[0];
            string   argument   = (arguments.Length > 1) ? arguments[1] : null;

            var optionDef = _optionStorage.GetOptionDefinition(optionName);

            if (TryLogShortOptionTooLong(isShort, optionName, argument))
            {
                return;
            }

            if (TryLogUnknownOption(optionDef, optionName, argument))
            {
                return;
            }

            if (TryLogOptionOverride(optionDef, optionName, argument))
            {
                return;
            }
            if (argument == null && optionDef.ArgumentPresence != ArgumentPresence.None)
            {
                if (StackTopIsValidParameter(tokens))
                {
                    argument = tokens.Pop();
                }

                if (TryLogMandatoryArgumentMissing(argument, optionName, isShort, optionDef))
                {
                    return;
                }
            }
            if (TryLogOptionDoesNotTakeAnArgument(argument, optionName, optionDef, isShort))
            {
                return;
            }


            OptionParsed optionParsed     = new OptionParsed(optionName, argument, isShort, optionDef.ValueParser);
            var          validationResult = optionDef.Validate(optionParsed);

            if (TryLogValidationError(validationResult, optionParsed))
            {
                return;
            }

            _result.AddParsedOption(optionParsed);
        }
Beispiel #3
0
        internal void AddParsedOption(OptionParsed option)
        {
            OptionDefinition definition = _optionDefinitions.GetOptionDefinition(option.Name);

            if (definition == null)
            {
                throw new ArgumentException("OptionDefinition with provided name does not exist in OptionStorage");
            }
            if (_pairedOptions.ContainsKey(definition))
            {
                _errors.Add(new ParseError(ParseErrorKind.DuplicitOption, option.Name, option.Value));
                return;
            }
            _pairedOptions.Add(definition, option);
            _parsedOptions.Add(option);
        }
 private static void AddError(ParseErrorKind errorKind, OptionParsed optionParsed)
 {
     AddError(errorKind, optionParsed.Name, optionParsed.Value);
 }
 private static bool TryLogValidationError(ValueValidationResult validationResult, OptionParsed optionParsed)
 {
     if (validationResult == ValueValidationResult.RangeError)
     {
         AddError(ParseErrorKind.ArgumentRangeMismatch, optionParsed);
         return(true);
     }
     if (validationResult == ValueValidationResult.TypeError)
     {
         AddError(ParseErrorKind.ArgumentTypeMismatch, optionParsed);
         return(true);
     }
     return(false);
 }