Пример #1
0
        private static bool TryConvertValueToChoice(IEnumerable <string> values, ChoiceTemplateParameter parameter, out string parsedValue, out string error)
        {
            parsedValue = string.Empty;
            error       = string.Empty;

            List <string> parsedValues = new List <string>();

            foreach (string val in values)
            {
                if (!TryConvertSingleValueToChoice(val, parameter, out string value, out error))
                {
                    return(false);
                }
                parsedValues.Add(value);
            }

            // An empty value is not allowed when multiple choice values are specified.
            if (parsedValues.Count > 1 && parsedValues.Any(string.IsNullOrEmpty))
            {
                error = CreateParseError(string.Empty, parameter);
                return(false);
            }

            parsedValue = string.Join(MultiValueParameter.MultiValueSeparator, parsedValues);
            return(true);
        }
Пример #2
0
 private static string CreateParseError(string value, ChoiceTemplateParameter parameter)
 {
     //value '{0}' is not allowed, allowed values are: {1}
     return(string.Format(
                LocalizableStrings.ParseChoiceTemplateOption_ErrorText_InvalidChoiceValue,
                value,
                string.Join(",", parameter.Choices.Keys.Select(key => $"'{key}'"))));
 }
 internal CombinedChoiceTemplateParameter(ChoiceTemplateParameter parameter) : base(parameter)
 {
     foreach (var choice in parameter.Choices)
     {
         if (!_combinedParameters.ContainsKey(choice.Key))
         {
             _combinedParameters[choice.Key] = choice.Value;
         }
     }
 }
        internal void MergeChoices(ChoiceTemplateParameter parameter)
        {
            if (parameter.Type != ParameterType.Choice)
            {
                throw new ArgumentException($"{nameof(parameter)} should have {nameof(parameter.Type)} {nameof(ParameterType.Choice)}");
            }
            if (parameter.Choices == null)
            {
                throw new ArgumentException($"{nameof(parameter)} should have {nameof(parameter.Choices)}");
            }

            foreach (var choice in parameter.Choices)
            {
                if (!_combinedParameters.ContainsKey(choice.Key))
                {
                    _combinedParameters[choice.Key] = choice.Value;
                }
            }
        }
Пример #5
0
        private static bool TryConvertSingleValueToChoice(string value, ChoiceTemplateParameter parameter, out string parsedValue, out string error)
        {
            parsedValue = string.Empty;
            error       = string.Empty;

            if (value.Equals(string.Empty))
            {
                return(true);
            }

            foreach (string choiceValue in parameter.Choices.Keys)
            {
                if (string.Equals(choiceValue, value, StringComparison.OrdinalIgnoreCase))
                {
                    parsedValue = choiceValue;
                    return(true);
                }
            }
            error = CreateParseError(value, parameter);
            return(false);
        }
Пример #6
0
        private static ParseArgument <string> GetParseChoiceArgument(ChoiceTemplateParameter parameter)
        {
            return((argumentResult) =>
            {
                if (argumentResult.Parent is not OptionResult or)
                {
                    throw new NotSupportedException("The method should be only used with option.");
                }

                if (argumentResult.Tokens.Count == 0)
                {
                    if (or.IsImplicit)
                    {
                        if (!string.IsNullOrWhiteSpace(parameter.DefaultValue))
                        {
                            if (TryConvertValueToChoice(parameter.DefaultValue, parameter, out string defaultValue, out string error))
                            {
                                return defaultValue;
                            }
                            //Cannot parse default value '{0}' for option '{1}' as expected type '{2}': {3}.
                            argumentResult.ErrorMessage = string.Format(
                                LocalizableStrings.ParseChoiceTemplateOption_Error_InvalidDefaultValue,
                                parameter.DefaultValue,
                                or.Token?.Value,
                                "choice",
                                error);
                            return string.Empty;
                        }
                        //Default value for argument missing for option: '{0}'.
                        argumentResult.ErrorMessage = string.Format(LocalizableStrings.ParseTemplateOption_Error_MissingDefaultValue, or.Token?.Value);
                        return string.Empty;
                    }
                    if (parameter.DefaultIfOptionWithoutValue != null)
                    {
                        if (TryConvertValueToChoice(parameter.DefaultIfOptionWithoutValue, parameter, out string defaultValue, out string error))
                        {
                            return defaultValue;
                        }
                        //Cannot parse default if option without value '{0}' for option '{1}' as expected type '{2}': {3}.
                        argumentResult.ErrorMessage = string.Format(
                            LocalizableStrings.ParseChoiceTemplateOption_Error_InvalidDefaultIfNoOptionValue,
                            parameter.DefaultIfOptionWithoutValue,
                            or.Token?.Value,
                            "choice",
                            error);
                        return string.Empty;
                    }
                    //Required argument missing for option: '{0}'.
                    argumentResult.ErrorMessage = string.Format(LocalizableStrings.ParseTemplateOption_Error_MissingDefaultIfNoOptionValue, or.Token?.Value);
                    return string.Empty;
                }
                else if (!parameter.AllowMultipleValues && argumentResult.Tokens.Count != 1)
                {
                    //Using more than 1 argument is not allowed for '{0}', used: {1}.
                    argumentResult.ErrorMessage = string.Format(LocalizableStrings.ParseTemplateOption_Error_InvalidCount, or.Token?.Value, argumentResult.Tokens.Count);
                    return string.Empty;
                }
                else
                {
                    if (!TryConvertValueToChoice(argumentResult.Tokens.Select(t => t.Value), parameter, out string value, out string error))
                    {
                        //Cannot parse argument '{0}' for option '{1}' as expected type '{2}': {3}.
                        argumentResult.ErrorMessage = string.Format(
                            LocalizableStrings.ParseChoiceTemplateOption_Error_InvalidArgument,
                            argumentResult.Tokens[0].Value,
                            or.Token?.Value,
                            "choice",
                            error);
                        return string.Empty;
                    }

                    return value;
                }
            });
        }
Пример #7
0
 internal ChoiceTemplateParameter(ChoiceTemplateParameter choiceTemplateParameter)
     : base(choiceTemplateParameter)
 {
     _choices = choiceTemplateParameter.Choices.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, StringComparer.OrdinalIgnoreCase);
 }
Пример #8
0
 private static bool TryConvertValueToChoice(string value, ChoiceTemplateParameter parameter, out string parsedValue, out string error)
 {
     return(TryConvertValueToChoice(value.TokenizeMultiValueParameter(), parameter, out parsedValue, out error));
 }