/// <summary> /// Checks this instance. /// </summary> /// <param name="optionSet">The set of options to consider.</param> /// <param name="optionSpecificationSet">The set of option specifications to consider.</param> /// <param name="allowMissingItems">Indicates whether the items can be missing.</param> /// <returns>Returns the log of check.</returns> public static IBdoLog Check(this IDataElementSet optionSet, IOptionSpecSet optionSpecificationSet, bool allowMissingItems = false) { var log = new BdoLog(); if (optionSet?.Items != null && optionSpecificationSet != null) { if (!allowMissingItems) { foreach (IDataSpecification optionSpecification in optionSpecificationSet.Items.Where(p => p.RequirementLevel == RequirementLevels.Required)) { if (!optionSet.HasItem(optionSpecification.Name)) { log.AddError("Option '" + optionSpecification.Name + "' missing"); } } } foreach (IScalarElement option in optionSet.Items) { if (option?.Specification != null) { switch (option.Specification.ItemRequirementLevel) { case RequirementLevels.Required: if (string.IsNullOrEmpty(option.Items[0] as string)) { log.AddError("Option '" + option.Name + "' requires value"); } break; case RequirementLevels.Forbidden: if (!string.IsNullOrEmpty(option.Items[0] as string)) { log.AddError("Option '" + option.Name + "' does not allow value"); } break; } } } } return(log); }
// ------------------------------------------ // ACCESSORS // ------------------------------------------ #region Accessors // Arguments -------------------------------- /// <summary> /// Retrieves the arguments from the specified argument string values. /// </summary> /// <param name="arguments">The argument string values to consider.</param> /// <param name="optionSpecificationSet">The option specification set to consider.</param> /// <param name="allowMissingItems">Indicates whether the items can be missing.</param> /// <param name="log">The log to consider.</param> /// <returns>Returns the log of argument building.</returns> public static IOptionSet UpdateOptions( this string[] arguments, IOptionSpecSet optionSpecificationSet, bool allowMissingItems = false, IBdoLog log = null) { IOptionSet optionSet = new OptionSet(); int index = 0; if (arguments != null) { while (index < arguments.Length) { string currentArgumentString = arguments[index]; if (currentArgumentString != null) { IScalarElement argument = null; OptionSpec argumentSpecification = null; int aliasIndex = -2; if (optionSpecificationSet != null) { argumentSpecification = optionSpecificationSet.Items .Find(p => p.IsArgumentMarching(currentArgumentString, out aliasIndex)) as OptionSpec; } if (optionSpecificationSet == null || (argumentSpecification == null && allowMissingItems)) { argument = ElementFactory.CreateScalar(currentArgumentString, DataValueTypes.Text); argument.WithItems(arguments.GetStringAtIndex(index)); optionSet.Add(argument); } else if (argumentSpecification != null && optionSpecificationSet != null) { if (argumentSpecification.ValueType == DataValueTypes.Any) { argumentSpecification.ValueType = DataValueTypes.Text; } argument = ElementFactory.CreateScalar(argumentSpecification.Name, null, argumentSpecification.ValueType, argumentSpecification); argument.Specification = argumentSpecification; if (argumentSpecification.ItemRequirementLevel.IsPossible()) { if (argumentSpecification.Name.Contains(StringHelper.__PatternEmptyValue)) { argument.Name = argumentSpecification.Name.ToSubstring(0, argumentSpecification.Name.Length - StringHelper.__PatternEmptyValue.Length - 2); int valueIndex = -1; if (aliasIndex == -1) { valueIndex = argumentSpecification.Name.IndexOf(StringHelper.__PatternEmptyValue); } else if (aliasIndex > -1) { valueIndex = argumentSpecification.Aliases[aliasIndex].IndexOf(StringHelper.__PatternEmptyValue); } argument.WithItems(valueIndex < 0 ? string.Empty : currentArgumentString.ToSubstring(valueIndex)); } else { index++; if (index < arguments.Length) { argument.WithItems(arguments.GetStringAtIndex(index)); } } } optionSet.Add(argument); } } index++; } } optionSet.Check(optionSpecificationSet).AddEventsTo(log); return(optionSet); }
// Add subset ------------------------------- /// <summary> /// Adds the specified set of option specifications. /// </summary> /// <param name="subSet">The sub set to add.</param> public IOptionSpecSet AddSubSet(IOptionSpecSet subSet) { SubSets?.Add(subSet as OptionSpecSet); return(this); }