/// <summary> /// Load value of an IArg. This function commonly not called by end user. It is used for internal purpose. /// </summary> /// <param name="args">The IArg object.</param> /// <param name="getArgStr">The delegate of getting the argument value string from configuration source, say, command line, config file. /// The 1st string parameter is the property name. The 2nd string parameter is the default value. Return value is the string value. /// </param> /// <param name="checkStandAloneArg">The delegate of getting the argument value for the standalone argument. /// The 1st string parameter is the property name. The return value indicates the arguement show up or not. /// </param> /// <returns>Indicate the loading argument success or not.</returns> public static bool LoadArg(this IArg args, Func <string, string, string> getArgStr, Func <string, bool> checkStandAloneArg = null) { if ((null == args) || (null == getArgStr)) { return(false); } Type t = args.GetType(); var ps = t.GetProperties(); foreach (var p in ps) { object[] attrs = p.GetCustomAttributes(typeof(ArgInfAttribute), false); if ((null == attrs) || (0 == attrs.Length)) { continue; } ArgInfAttribute ia = attrs[0] as ArgInfAttribute; string key; int index = p.Name.LastIndexOf('.'); key = (index >= 0) ? p.Name.Substring(index) : p.Name; string argStr = null; if (ia.AType == ArgInfAttribute.ArgType.KeyValue) { if ((args.ArgInited) || (ia.Mandatory)) { argStr = getArgStr(key, null); } else { argStr = getArgStr(key, ia.Default); } } else //if ( ia.AType == ArgInfAttribute.ArgType.StandAlone ) { try { argStr = checkStandAloneArg(key).ToString(); } catch { } } if ((null != argStr) && (argStr.Length > 0)) { if (!args.SetValFromString(p, argStr)) { return(false); } ia.Mandatory = false; } } args.ArgInited = true; return(true); }
/// <summary> /// Check if all mandate properties of an IArg are initialized. /// </summary> /// <param name="arg">The IArg to be checked.</param> /// <returns>True if all mandate properties are initialized.</returns> public static bool IsMandateInited(this IArg arg) { Type t = arg.GetType(); var ps = t.GetProperties(); foreach (var p in ps) { Object[] atts = p.GetCustomAttributes(typeof(ArgInfAttribute), false); foreach (object o in atts) { ArgInfAttribute attr = o as ArgInfAttribute; if ((null != attr) && (attr.Mandatory)) { return(false); } } } return(true); }
/// <summary> /// Get the help information of an IArg. /// </summary> /// <param name="arg">The IArg object.</param> /// <returns></returns> public static string GetHelpInfo(this IArg arg) { StringBuilder helpInfo = new StringBuilder(); Type t = arg.GetType(); var ps = t.GetProperties(); foreach (var p in ps) { Object[] atts = p.GetCustomAttributes(typeof(ArgInfAttribute), false); if (atts.Length > 0) { string s; int index = p.Name.LastIndexOf('.'); s = (index >= 0) ? p.Name.Substring(index) : p.Name; foreach (object o in atts) { ArgInfAttribute attr = o as ArgInfAttribute; if (null != attr) { if (attr.AType == ArgInfAttribute.ArgType.KeyValue) { helpInfo.Append("-"); helpInfo.Append(s); helpInfo.Append(" :[Type "); helpInfo.Append(p.PropertyType.Name); helpInfo.Append("] \n "); helpInfo.Append(attr.HelpInf); } else { helpInfo.Append(s); helpInfo.Append(":\n "); helpInfo.Append(attr.HelpInf); } helpInfo.Append("\n"); } } } } return(helpInfo.ToString()); }