/// <summary> /// Initializes the class to represent a named parameter. /// </summary> /// <param name="pName">Name of the parameter</param> /// <param name="pHelp">Help message</param> /// <param name="pRole">Named or Passed parameter</param> /// <param name="pType">Value type converter</param> /// <param name="pScope">Scope of the parameter</param> /// <param name="pMultiplicity">Number of occurrences</param> /// <exception cref="SyntaxErrorException">Thrown if there is an invalid parameter.</exception> public Description(string pName, string pHelp, eROLE pRole, iParamType pType, eSCOPE pScope, eMULTIPLICITY pMultiplicity) { if (string.IsNullOrWhiteSpace(pName)) { throw new SyntaxErrorException(Errors.DescriptionName); } if (string.IsNullOrWhiteSpace(pHelp)) { throw new SyntaxErrorException(Errors.DescriptionHelp); } if (pRole == eROLE.NAMED && pType == null && pMultiplicity == eMULTIPLICITY.MULTIPLE) { throw new SyntaxErrorException(Errors.DescriptionSingle); } if (pRole == eROLE.PASSED && pType == null) { throw new SyntaxErrorException(Errors.DescriptionTypeRequired); } if (!Regex.IsMatch(pName, @"^[a-z_]\w*$", RegexOptions.IgnoreCase)) { throw new SyntaxErrorException(Errors.DescriptionInvalidName); } Name = pName.ToLower(); Help = pHelp; Role = pRole; Type = pType; Scope = pScope; Multiplicity = pMultiplicity; }
/// <summary> /// Converts the pattern for a single parameter description into an /// initialized description object. /// </summary> /// <param name="pOptions">Parsing options to use.</param> /// <param name="pHelpProvider"></param> /// <param name="pPattern">A string containing the syntax of a single argument.</param> /// <returns>A description object</returns> /// <exception cref="SyntaxErrorException"></exception> public static Description Parse(CliOptions pOptions, iHelpProvider pHelpProvider, string pPattern) { if (string.IsNullOrWhiteSpace(pPattern)) { throw new SyntaxErrorException(Errors.DescriptionFactoryNoPattern); } string pattern = pPattern.Trim(); eSCOPE scope = pattern.StartsWith("[") && pattern.EndsWith("]") ? eSCOPE.OPTIONAL : eSCOPE.REQUIRED; pattern = scope == eSCOPE.OPTIONAL ? pattern.Substring(1, pattern.Length - 2) : pattern; eROLE role = pattern.StartsWith(pOptions.Prefix) ? eROLE.NAMED : eROLE.PASSED; pattern = role == eROLE.NAMED ? pattern.Substring(pOptions.Prefix.Length) : pattern; eMULTIPLICITY multi = pattern.EndsWith("#") ? eMULTIPLICITY.MULTIPLE : eMULTIPLICITY.ONCE; pattern = multi == eMULTIPLICITY.MULTIPLE ? pattern.Substring(0, pattern.Length - 1) : pattern; int equal = pattern.IndexOf(pOptions.EqualChar, StringComparison.Ordinal); string type = equal == -1 ? null : pattern.Substring(equal + 1).ToLower(); string name = equal == -1 ? pattern : pattern.Substring(0, equal); iParamType paramType = type == null ? null : ParamTypeFactory.Create(type); if (paramType == null && role == eROLE.PASSED) { paramType = new ParamString(); } if (string.IsNullOrWhiteSpace(name)) { throw new SyntaxErrorException(Errors.DescriptionName); } string help = pHelpProvider.Get(name); return(new Description(name, help, role, paramType, scope, multi)); }