Esempio n. 1
0
        /// <summary>
        /// Returns the synopsis for the value of this option depending on the flag of value . <para> </para>
        /// If value for this option can be optional, her value flag is not <see cref="DrCmdValueFlags.Required"/> the function will be return value synopsis in square brackets <para> </para>
        /// If this option cannot contains value , her value flag is <see cref="DrCmdValueFlags.Forbidden"/> the function will be return empty string
        /// </summary>
        /// <returns></returns>
        internal string GetOptionValueSynopsis()
        {
            var text = string.Empty;

            if (!ValueFlags.HasFlag(DrCmdValueFlags.Forbidden)) // this option can have value
            {
                text = GetAttributeValue(DrCmdOptionSettings.SynopsisValue, string.Empty);
                if (!ValueFlags.HasFlag(DrCmdValueFlags.Required))
                {
                    text = "[" + text + "]";                                                // this value is optional
                }
            }
            return(text);
        }
Esempio n. 2
0
        /// <summary>
        /// Checking this option specified arguments.
        /// If arguments are incongruous the <exception cref="ArgumentException">ArgumentException</exception> will be thrown.
        /// </summary>
        public void ValidateOptionParameter()
        {
            // start validate value Type
            if (Option.Attributes.Contains(DrCmdOptionSettings.ValueType))  // validate specified Type for convert
            {
                var valueType = System.Type.GetType(Option.Attributes[DrCmdOptionSettings.ValueType].GetValueAsString());
                if (!DDValue.ValidateType(valueType))
                {
                    throw new ArgumentException(string.Format(Msg.SPECIFIED_VALUE_TYPE_IS_NOT_SUPPORTED_BY_DDVALUE, valueType.ToString()));
                }
            }
            // end validate value Type
            var isSpecifiedOption = GetAttributeValue(DrCmdOptionSettings.ResultIsOptionSpecified, false);

            if ((Type.HasFlag(DrCmdOptionType.Required)) & (isSpecifiedOption == false))
            {
                throw new ArgumentException(string.Format(Msg.REQUIRED_OPTION_IS_NOT_SPECIFIED, Name, CommandName));
            }
            // value flag
            var valueAsStringArray = GetAttributeValue(DrCmdOptionSettings.ResultValueAsStringArray, new string[] { }).GetValueAsStringArray();
            var valueAsString      = GetAttributeValue(DrCmdOptionSettings.ResultValueAsStringArray, string.Empty).GetValueAsString();

            // Single
            if ((ValueFlags.HasFlag(DrCmdValueFlags.Single)) & (valueAsStringArray.Length > 1))
            {
                throw new ArgumentException(string.Format(Msg.OPTION_CANNOT_CONTAINS_MORE_THAN_ONE_VALUE, Name, valueAsString, valueAsStringArray.Length, DrCmdValueFlags.Single, CommandName));
            }
            // Forbidden
            if ((ValueFlags.HasFlag(DrCmdValueFlags.Forbidden)) & (valueAsStringArray.Length > 0))
            {
                throw new ArgumentException(string.Format(Msg.OPTION_CANNOT_CONTAIN_VALUE, Name, valueAsString, DrCmdValueFlags.Forbidden, CommandName));
            }
            // Required
            if ((isSpecifiedOption) && (ValueFlags.HasFlag(DrCmdValueFlags.Required)) & (valueAsStringArray.Length == 0))
            {
                throw new ArgumentException(string.Format(Msg.OPTION_MUST_CONTAIN_VALUE, Name, DrCmdValueFlags.Required, CommandName));
            }
            if ((ValueFlags.HasFlag(DrCmdValueFlags.ListOfRestriction)) && (valueAsStringArray.Length > 0))
            {
                ValidateOptionValueByRestrictionList(valueAsStringArray);                                                                                             // RestrictionList and AllowNumeric
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Checks the value of the options on their compliance with the list of allowed values ​​or, if enabled, their numerical conform
        /// If the option value is incorrect by restriction list from attribute <see cref="DrCmdOptionSettings.RestrictionList"/> the <exception cref="ArgumentException">ArgumentException</exception> will be thrown.
        /// </summary>
        /// <param name="valueAsStringArray">option values as string array</param>
        private void ValidateOptionValueByRestrictionList(IEnumerable <string> valueAsStringArray)
        {
            var restrictionListArray = GetAttributeValue(DrCmdOptionSettings.RestrictionList, new string[] { }).GetValueAsStringArray();

            var ignoreCase = !Command.Settings.GetSettingsCaseSensitive();

            foreach (var val in valueAsStringArray)
            {
                if ((ValueFlags.HasFlag(DrCmdValueFlags.AllowNumeric)) && (val.IsPositiveNumber()))
                {
                    continue;
                }
                var isMatched = false;
                foreach (var restrictionItem in restrictionListArray)
                {
                    if (string.Compare(restrictionItem, val, ignoreCase) == 0)
                    {
                        isMatched = true;
                        break;
                    }
                }
                if (!isMatched)
                {
                    var text = string.Format(Msg.OPTION_HAS_INCORRECT_VALUE, Name, val, CommandName) + " ";
                    if (ValueFlags.HasFlag(DrCmdValueFlags.AllowNumeric))
                    {
                        text += string.Format(Msg.OPTION_SUPPORT_ONLY_FOLLOWING_VALUES_OR_NUMERIC, GetRestrictionListWithValueAsString(", "));
                    }
                    else
                    {
                        text += string.Format(Msg.OPTION_SUPPORT_ONLY_FOLLOWING_VALUES, GetRestrictionListAsString(", "));
                    }
                    throw new ArgumentException(text);
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Verify flag of value for option.
 /// In the case of the incongruous flags are detected ​​the <exception cref="FormatException">FormatException</exception>will be thrown
 /// </summary>
 public void VerifyValueRestrictionsType()
 {
     try
     {
         #region Forbidden
         if ((ValueFlags.HasFlag(DrCmdValueFlags.Forbidden)) && (ValueFlags != DrCmdValueFlags.Forbidden))
         {
             throw new FormatException(string.Format(Msg.WITH_THIS_TYPE_CANNOT_BE_SPECIFIED_MORE_THAN_OTHER_TYPES, DrCmdValueFlags.Forbidden));
         }
         #endregion Forbidden
         if ((ValueFlags.HasFlag(DrCmdValueFlags.AllowNumeric)) && (!ValueFlags.HasFlag(DrCmdValueFlags.ListOfRestriction)))
         {
             throw new FormatException(string.Format(Msg.CANNOT_SPECIFY_ONE_TYPE_WITHOUT_SECOND_TYPE, DrCmdValueFlags.AllowNumeric, DrCmdValueFlags.ListOfRestriction)); // AllowNumeric without ListOfRestriction
         }
         var restrictionList            = GetAttributeValue(DrCmdOptionSettings.RestrictionList, new string[] { }).GetValueAsStringArray();
         var restrictionListDescription = GetAttributeValue(DrCmdOptionSettings.RestrictionListDescription, new string[] { }).GetValueAsStringArray();
         if (ValueFlags.HasFlag(DrCmdValueFlags.AllowNumeric)) // if AllowNumeric is true need to check both list element count -> RestrictionList and RestrictionListAsNumeric
         {
             var restrictionListAsNumeric = GetAttributeValue(DrCmdOptionSettings.RestrictionListAsNumeric, new int[] { }).GetValueAsIntArray();
             if (restrictionList.Length != restrictionListAsNumeric.Length)
             {
                 throw new FormatException(string.Format(Msg.ELEMENT_COUNT_IN_RESTRICTION_LIST_IS_NOT_EQUALS_OF_NUMERIC_LIST_COUNT, Name, restrictionList.Length.ToString(), restrictionListAsNumeric.Length.ToString()));
             }
         }
         if ((restrictionList.Length > 0) && (restrictionListDescription.Length > 0) && (restrictionList.Length != restrictionListDescription.Length))
         {
             throw new FormatException(string.Format(Msg.ELEMENT_COUNT_IN_RESTRICTION_LIST_IS_NOT_EQUALS_OF_RESTRICTIOM_DESCRIPTION_LIST, Name, restrictionList.Length.ToString(), restrictionListDescription.Length.ToString()));
         }
         CheckIncongruousTypeOfValue(ValueFlags, DrCmdValueFlags.Optional, DrCmdValueFlags.Required);                                                             // Optioanl & Required
         CheckIncongruousTypeOfValue(ValueFlags, DrCmdValueFlags.Single, DrCmdValueFlags.List);                                                                   // Single & List
     }
     catch (ApplicationException e)
     {
         throw new FormatException(string.Format(Msg.OPTION_TYPE_IS_WRONG, Name, ValueFlags.ToString()), e);
     }
 }