コード例 #1
0
        /// <summary>
        /// Parses the argument.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="argument">The argument.</param>
        /// <returns>The argument parsing result</returns>
        /// <exception cref="System.ArgumentException">argument or option Object cannot be null</exception>
        public static ArgumentResult ParseArgument(string[] args, RulePerfConsoleArgument argument)
        {
            Debug.Assert(args != null, "Argument should not be null");
            if (argument == null)
            {
                throw new ArgumentException("argument or optionObject cannot be null");
            }

            return(ParseArgumentInternal(args, argument));
        }
コード例 #2
0
        /// <summary>
        /// Parses the argument internal.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <param name="argumentObject">The argument object.</param>
        /// <returns>Argument parsing result</returns>
        /// <exception cref="System.ArgumentException">IArgumentObject.Options cannot be null or empty</exception>
        internal static ArgumentResult ParseArgumentInternal(string[] args, RulePerfConsoleArgument argumentObject)
        {
            // Validate IArgumentObject.Options
            var options = argumentObject.Options;

            if (options == null || options.Count == 0)
            {
                throw new ArgumentException("IArgumentObject.Options cannot be null or empty");
            }

            if (options.Select(a => a.Key.ToLower()).Distinct().Count() != options.Count)
            {
                throw new ArgumentException("IArgumentObject.Options has duplicated option name");
            }

            // Retrieve the attributes from object's properties
            var optionParameterAttribDic = BuildArguParamAttrDic(options.Keys);

            // Parse system commandline, after this line, no exception is thrown to caller.
            ArgumentResult ar = new ArgumentResult();

            ar.ParamAttributes    = optionParameterAttribDic;
            ar.OptionDescriptions = options;
            List <KeyValuePair <string, string> > argPairs = null;

            try
            {
                string optionName = ParseArgumentArray(args, out argPairs);
                if (!optionParameterAttribDic.ContainsKey(optionName))
                {
                    throw new ArgumentException("optionName: '" + optionName + "' is not defined in argument object");
                }

                ar.SelectedOptionName = optionName;

                ////#region Settings for an option
                argumentObject.BuildStep(ar.SelectedOptionName);

                CommonStep commonStep = new CommonStep();

                foreach (Step step in argumentObject.Steps)
                {
                    // Specific step's settings
                    string[] settingNames = step.SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    List <SettingEntityModel> settings = SettingEntityModel.Get(settingNames);
                    foreach (SettingEntityModel setting in settings)
                    {
                        ArgumentParameterAttribute attr = new ArgumentParameterAttribute(argumentObject.GetOptionName(step));
                        attr.DefaultValue  = setting.SettingValue;
                        attr.Delimiter     = '|';
                        attr.Description   = setting.SettingName;
                        attr.MaxOccur      = 1;
                        attr.MinOccur      = 0;
                        attr.ParameterName = setting.SettingName;

                        foreach (string option in attr.OptionsBindTo)
                        {
                            if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                            {
                                throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}", option, attr.ParameterName));
                            }

                            optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                        }
                    }

                    // Common settings
                    settingNames = commonStep.SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    settings     = SettingEntityModel.Get(settingNames);
                    foreach (SettingEntityModel setting in settings)
                    {
                        ArgumentParameterAttribute attr = new ArgumentParameterAttribute(argumentObject.GetOptionName(step));
                        attr.DefaultValue  = setting.SettingValue;
                        attr.Delimiter     = '|';
                        attr.Description   = setting.SettingName;
                        attr.MaxOccur      = 1;
                        attr.MinOccur      = 0;
                        attr.ParameterName = setting.SettingName;

                        foreach (string option in attr.OptionsBindTo)
                        {
                            if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                            {
                                // If the setting has been set by the specifical step, then ignore the global setting process
                                // throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}",
                                //    option, attr.ParameterName));
                            }
                            else
                            {
                                optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                            }
                        }
                    }
                }

                /*
                 * foreach (var property in argumentObject.GetType().GetProperties())
                 * {
                 *  var paramAttrs = property.GetCustomAttributes<ArgumentParameterAttribute>();
                 *  if (paramAttrs.Length > 0) // Only validate property with ParameterOptionAttribute
                 *  {
                 *      if (!property.PropertyType.IsSupported())
                 *      {
                 *          throw new ArgumentException(string.Format("Property:{0}, the Type:{1} is not supported",
                 *              property.Name,
                 *              property.PropertyType.Name));
                 *      }
                 *
                 *      foreach (var attr in paramAttrs)
                 *      {
                 *          ValidateParameterOptionAttr(property, attr, options);
                 *
                 *          foreach (string option in attr.OptionsBindTo)
                 *          {
                 *              if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                 *              {
                 *                  throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}",
                 *                      option, attr.ParameterName));
                 *              }
                 *              optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                 *          }
                 *      }
                 *  }
                 * }*/
                ////#endregion Settings for an option

                AssignValuesToArgumentObject(argumentObject, ar.SelectedOptionName, argPairs, optionParameterAttribDic[ar.SelectedOptionName]);
            }
            catch (ArgumentException ae)
            {
                ExceptionHelper.CentralProcess(ae);
                ar.ErrorMessages.Add(ae.Message);
                ar.ParseSucceeded = false;
                return(ar);
            }

            ar.ParseSucceeded = true;
            return(ar);
        }