Esempio n. 1
0
        /// <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;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates descriptions of parameters from the public property members of an
        /// object.
        /// </summary>
        /// <typeparam name="T">The object type to inspect.</typeparam>
        /// <param name="pOptions">The parser option</param>
        /// <param name="pInfos">Collection of properties</param>
        /// <returns>Parameter descriptions in syntax format.</returns>
        private static string ReflectDescriptions <T>(CliOptions pOptions, IEnumerable <PropertyInfo> pInfos)
            where T : class, new()
        {
            List <string> desc = new List <string>();

            foreach (PropertyInfo info in pInfos)
            {
                eROLE  role = eROLE.PASSED;
                string name = info.Name.ToLower();

                CliName attrName = Attribute <CliName>(info);
                if (attrName != null)
                {
                    role = attrName.Role;
                    name = attrName.Name ?? name;
                }

                string str = string.Format(
                    "{0}{1}:{2}",
                    role == eROLE.NAMED ? pOptions.Prefix : "",
                    name,
                    info.PropertyType.Name.ToLower()
                    );

                CliOptional attrOptional = Attribute <CliOptional>(info);
                if (attrOptional != null)
                {
                    str = string.Format("[{0}]", str);
                }

                desc.Add(str);
            }
            return(string.Join(" ", desc));
        }
Esempio n. 3
0
        /// <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;
        }
Esempio n. 4
0
        /// <summary>
        /// Formats an error message.
        /// </summary>
        /// <param name="pRole">Parameter role</param>
        /// <param name="pPrefix">Parameter prefix</param>
        /// <param name="pName">Parameter name</param>
        /// <param name="pError">Error message</param>
        /// <returns>The formatted string</returns>
        private static string getError(eROLE pRole, string pPrefix, string pName, string pError)
        {
            string app = AppName();

            return(pRole == eROLE.NAMED
                ? string.Format("{0}: option '{1}{2}' {3}.", app, pPrefix, pName, pError)
                : string.Format("{0}: value <{1}> {2}.", app, pName, pError));
        }
Esempio n. 5
0
        /// <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));
        }
Esempio n. 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="pRole"></param>
 /// <param name="pName">The name of the parameter</param>
 public CliName(eROLE pRole = eROLE.NAMED, string pName = null)
 {
     Role = pRole;
     Name = pName;
 }
Esempio n. 7
0
 /// <summary>
 /// Formats a message based upon a parameter description.
 /// </summary>
 /// <param name="pRole">The parameter's role.</param>
 /// <param name="pPrefix">Prefix used for named parameters.</param>
 /// <param name="pName">The name of the parameter</param>
 public static string WriteRequired(eROLE pRole, string pPrefix, string pName)
 {
     return(getError(pRole, pPrefix, pName, "is required"));
 }
Esempio n. 8
0
 /// <summary>
 /// Formats a message based upon a parameter description.
 /// </summary>
 /// <param name="pRole">The parameter's role.</param>
 /// <param name="pPrefix">Prefix used for named parameters.</param>
 /// <param name="pName">The name of the parameter</param>
 public static string WriteMissingValue(eROLE pRole, string pPrefix, string pName)
 {
     return(getError(pRole, pPrefix, pName, "is missing value"));
 }
Esempio n. 9
0
 /// <summary>
 /// Formats a message based upon a parameter description.
 /// </summary>
 /// <param name="pRole">The parameter's role.</param>
 /// <param name="pPrefix">Prefix used for named parameters.</param>
 /// <param name="pName">The name of the parameter</param>
 public static string WriteDuplicate(eROLE pRole, string pPrefix, string pName)
 {
     return(getError(pRole, pPrefix, pName, "can only be used once"));
 }