private static void SetMetaDataSection(ChoCommandLineArgObject cmdLineArgObject, ChoPropertyInfos propertyInfos) { return; if (cmdLineArgObject == null) { return; } string elementPath = cmdLineArgObject.GetType().Name; using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_metaDataFilepath)) { if (elementPath.IsNullOrEmpty()) { return; } XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(elementPath); if (node == null) { node = xmlDocument.XmlDocument.MakeXPath(elementPath); } if (node != null) { xmlDocument.XmlDocument.InnerXml = ChoXmlDocument.AppendToInnerXml(node, propertyInfos.ToXml()); xmlDocument.Save(); } } }
private static ChoPropertyInfos GetPropertyInfos(ChoCommandLineArgObject cmdLineArgObject) { string elementPath = cmdLineArgObject.GetType().Name; if (elementPath.IsNullOrWhiteSpace()) { return(null); } Dictionary <string, ChoPropertyInfos> propDict = _propDict; if (!propDict.ContainsKey(elementPath)) { lock (_padLock) { if (!propDict.ContainsKey(elementPath)) { string xpath = @"//{0}/propertyInfos".FormatString(elementPath); XmlNode node = null; if (_rootNode != null) { node = _rootNode.SelectSingleNode(xpath); } if (node != null) { propDict.Add(elementPath, ChoPropertyInfos.Default); ChoPropertyInfos propertyInfos = node.ToObject(typeof(ChoPropertyInfos)) as ChoPropertyInfos; propertyInfos.Initialize(); propDict[elementPath] = propertyInfos; } else { propDict[elementPath] = ConstructPropertyInfos(cmdLineArgObject); } } } } return(propDict.ContainsKey(elementPath) ? propDict[elementPath] : null); }
private static ChoPropertyInfos ConstructPropertyInfos(ChoCommandLineArgObject cmdLineArgObject) { if (cmdLineArgObject != null) { Dictionary <string, ChoPropertyInfos> propDict = _propDict; string elementPath = cmdLineArgObject.GetType().Name; if (elementPath.IsNullOrWhiteSpace()) { return(ChoPropertyInfos.Default); } if (!propDict.ContainsKey(elementPath)) { lock (_padLock) { if (!propDict.ContainsKey(elementPath)) { MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(cmdLineArgObject.GetType()); if (memberInfos != null && memberInfos.Length > 0) { //Set member values List <ChoPropertyInfo> propertyInfoList = new List <ChoPropertyInfo>(); ChoDefaultCommandLineArgAttribute memberInfoAttribute = null; foreach (MemberInfo memberInfo in memberInfos) { memberInfoAttribute = (ChoDefaultCommandLineArgAttribute)ChoType.GetMemberAttributeByBaseType(memberInfo, typeof(ChoDefaultCommandLineArgAttribute)); if (memberInfoAttribute == null) { continue; } ChoPropertyInfo propInfo = new ChoPropertyInfo(); propInfo.Name = ChoType.GetMemberName(memberInfo); if (ChoType.GetMemberType(memberInfo) != typeof(bool)) { propInfo.DefaultValue = new ChoCDATA(memberInfoAttribute.DefaultValue); propInfo.FallbackValue = new ChoCDATA(memberInfoAttribute.FallbackValue); propInfo.IsDefaultValueSpecified = memberInfoAttribute.IsDefaultValueSpecified; propInfo.IsFallbackValueSpecified = memberInfoAttribute.IsFallbackValueSpecified; propertyInfoList.Add(propInfo); } } if (propertyInfoList != null) { ChoPropertyInfos propertyInfos = new ChoPropertyInfos(); propertyInfos.PropertyInfoArr = propertyInfoList.ToArray(); SetMetaDataSection(cmdLineArgObject, propertyInfos); return(propertyInfos); } } } } } } return(ChoPropertyInfos.Default); }
public static void Load(object target, string[] commandLineArgs) { ChoGuard.ArgumentNotNull(target, "Target"); ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject; if (commandLineArgObject == null) { throw new ChoApplicationException("Target is not ChoCommandLineArgObject."); } ChoCommandLineArgObjectAttribute commandLineArgumentsObjectAttribute = commandLineArgObject.GetType().GetCustomAttribute(typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute; if (commandLineArgumentsObjectAttribute == null) { throw new ChoApplicationException("Missing ChoCommandLineArgObjectAttribute. Must be specified."); } ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; bool isUsageAvail = !commandLineArgumentsObjectAttribute.Silent; bool showUsageIfEmpty = commandLineArgumentsObjectAttribute.GetShowUsageIfEmpty(); if (commandLineArgs.IsNullOrEmpty() && showUsageIfEmpty) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage()); } } Exception exception = null; using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser()) { commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) => { if (commandLineArgObject != null) { commandLineArgObject.RaiseUnrecognizedCommandLineArgFound(eventArgs); } }); commandLineArgParser.Parse(commandLineArgs); if (commandLineArgObject != null) { commandLineArgObject.CommandLineArgs = commandLineArgs; } if ((commandLineArgParser.IsUsageArgSpecified && commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace()) || (!commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace() && HasUsageSwitchSpecified(commandLineArgParser, commandLineArgumentsObjectAttribute)) ) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage()); } } string cmdLineSwitch = null; if (commandLineArgObject == null || !commandLineArgObject.RaiseBeforeCommandLineArgObjectLoaded(commandLineArgs)) { MemberInfo[] memberInfos = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute)); MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoPositionalCommandLineArgAttribute)); memberInfos = memberInfos1.Concat(memberInfos).ToArray(); if (memberInfos != null && memberInfos.Length > 0) { foreach (MemberInfo memberInfo in memberInfos) { cmdLineSwitch = GetCmdLineSwitch(memberInfo); exception = ExtractNPopulateValue(commandLineArgObject, memberInfo, commandLineArgParser); if (isUsageAvail) { if (exception != null) { break; } } } } } if (commandLineArgObject != null) { if (exception != null) { if (!commandLineArgObject.RaiseCommandLineArgObjectLoadError(commandLineArgs, exception)) { if (isUsageAvail) { if (exception is ChoCommandLineArgException) { throw exception; } else { throw new ChoCommandLineArgException("Found exception while loading `{2}` command line argument. {0}{0}{1}".FormatString( Environment.NewLine, exception.Message, cmdLineSwitch), commandLineArgObject.GetUsage(), exception); } } } } else { commandLineArgObject.RaiseAfterCommandLineArgObjectLoaded(commandLineArgs); } } } }