Ejemplo n.º 1
0
        public ApplicationParameters(ApplicationParametersModel model, String[] args, Func <IList <AbstractApplicationParameter>, IList <ValidationError> > additionalValidator = null)
        {
            this._model = model;
            var   list             = new List <AbstractApplicationParameter>(model.Parameters.Count);
            Int32 idx              = 0;
            var   argsList         = args.ToList();
            var   argsListReadOnly = new ReadOnlyCollection <String>(argsList);
            var   errors           = new List <ValidationError>();
            var   i                        = 0;
            var   curParamCount            = 0;
            var   latestAddedParamModelIdx = -1;

            while (i < model.Parameters.Count && idx < argsList.Count)
            {
                var pModel   = model.Parameters[i];
                var instance = ProcessParameterModel(pModel, model, argsList, argsListReadOnly, errors, ref idx);
                if (instance != null)
                {
                    ++curParamCount;
                }

                var noInstanceOrTooBigIndex = instance == null || idx >= argsList.Count;
                if (noInstanceOrTooBigIndex || (pModel.MaxOccurrences >= 0 && curParamCount > pModel.MaxOccurrences))
                {
                    ++i; // Move to next parameter
                    if (noInstanceOrTooBigIndex && curParamCount < pModel.MinOccurrences)
                    {
                        errors.Add(new ValidationError(null, "Parameter " + pModel.Name + " occurred too few times."));
                    }
                    else if (!noInstanceOrTooBigIndex)
                    {
                        errors.Add(new ValidationError(null, "Parameter " + pModel.Name + " occurred too many times."));
                    }
                    else if (instance != null)
                    {
                        list.Add(instance);
                    }
                    curParamCount = 0;
                }
                else
                {
                    list.Add(instance);
                    latestAddedParamModelIdx = i;
                }
            }
            ++latestAddedParamModelIdx;

            if (latestAddedParamModelIdx < model.Parameters.Count)
            {
                errors.AddRange(model.Parameters.Skip(i).Where(p => p.MinOccurrences > 0).Select(p => new ValidationError(null, "Parameter " + p.Name + " occurred too few times.")));
            }

            this._parameters = new ReadOnlyCollection <AbstractApplicationParameter>(list);

            if (additionalValidator != null)
            {
                var aErrors = additionalValidator(this._parameters);
                if (aErrors != null)
                {
                    errors.AddRange(aErrors);
                }
            }

            this._errors = new ReadOnlyCollection <ValidationError>(errors);
        }
Ejemplo n.º 2
0
        private static AbstractApplicationParameter ProcessParameterModel(ApplicationParameterModel p, ApplicationParametersModel paramsModel, IList <String> args, IList <String> argsListReadOnly, IList <ValidationError> errors, ref Int32 idx)
        {
            var arg = args[idx];

            if (p.Expandable)
            {
                var eArgs = TryExpandArgumentsRecursively(paramsModel.ParameterParser, arg);
                if (eArgs != null)
                {
                    // Expanded successfully
                    args.RemoveAt(idx);
                    for (var i = eArgs.Count - 1; i >= 0; --i)
                    {
                        args.Insert(idx, eArgs[i]);
                    }
                    // Read-only list should reflect the changed list
                }
            }

            AbstractApplicationParameter pInstance = null;

            if (idx < args.Count)
            {
                arg = args[idx];
                foreach (var optionModel in p.PossibleOptions)
                {
                    String on, ov, errorMsg;
                    Object ovt;
                    var    oldIdx = idx;
                    idx += paramsModel.ParameterParser.TryParseOption(argsListReadOnly, idx, optionModel, out on, out ov, out ovt, out errorMsg);
                    if (idx > oldIdx)
                    {
                        pInstance = new ApplicationOptionParameter(p, optionModel, on, ov, ovt);
                        if (errorMsg != null)
                        {
                            errors.Add(new ValidationError(on, errorMsg));
                        }
                        break;
                    }
                }

                if (pInstance == null && p.CanBeNonOption)
                {
                    pInstance = new ApplicationValueParameter(p, arg);
                    idx      += 1;
                }
            }

            return(pInstance);
        }