public static void Load(object target, string[] commandLineArgs) { ChoGuard.ArgumentNotNull(target, "Target"); ChoCommandLineArgBuilder commandLineArgBuilder = target as ChoCommandLineArgBuilder; if (commandLineArgBuilder == null) { throw new ChoApplicationException("Target is not ChoCommandLineArgBuilder."); } using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser()) { //commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) => //{ // if (commandLineArgBuilder != null) // commandLineArgBuilder.RaiseUnrecognizedCommandLineArgFound(eventArgs); //}); commandLineArgParser.Parse(commandLineArgs); if (commandLineArgBuilder != null) { ChoEnvironment.CommandLineArgs = ChoEnvironment.CommandLineArgs.Skip(1).ToArray(); } bool isUsageAvail = true; ChoCommandLineArgObjectAttribute commandLineArgumentsObjectAttribute = commandLineArgBuilder.GetType().GetCustomAttribute(typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute; if (commandLineArgumentsObjectAttribute != null) { isUsageAvail = !commandLineArgumentsObjectAttribute.Silent; } if (commandLineArgParser.PosArgs.Length == 0 && commandLineArgParser.Switches.Count == 0) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgBuilder.GetUsage()); } } if (commandLineArgParser.PosArgs.Length == 0 && commandLineArgParser.IsUsageArgSpecified) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgBuilder.GetUsage()); } } string command = commandLineArgParser.PosArgs[0]; if (command.IsNullOrWhiteSpace()) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgBuilder.GetUsage()); } } Type commandLineArgObjectType = commandLineArgBuilder.GetCommandLineArgObjectType(command); if (commandLineArgObjectType == null) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException("Command '{1}' not found.{0}{0}{2}".FormatString(Environment.NewLine, command, commandLineArgBuilder.GetUsage())); } } try { ChoCommandLineArgObject argObj = ChoActivator.CreateInstance(commandLineArgObjectType) as ChoCommandLineArgObject; commandLineArgBuilder.CommandLineArgObject = argObj; } catch (ChoCommandLineArgUsageException uEx) { throw new ChoCommandLineArgUsageException(uEx.Message.Insert(uEx.Message.IndexOf(' '), " " + command)); } catch (ChoCommandLineArgException aEx) { throw new ChoCommandLineArgUsageException( "{1}{0}{0}{2}".FormatString(Environment.NewLine, aEx.ErrorMessage, aEx.UsageMessage.Insert(aEx.UsageMessage.IndexOf(' '), " " + command))); } } }
public static void Load(object target, string[] commandLineArgs) { ChoGuard.ArgumentNotNull(target, "Target"); ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject; Exception exception = null; if (commandLineArgObject != null) { commandLineArgObject.CommandLineArgs = commandLineArgs; } using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser(commandLineArgs)) { commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) => { if (commandLineArgObject != null) { commandLineArgObject.OnUnrecognizedCommandLineArgFound(eventArgs); } }); commandLineArgParser.Parse(); if (commandLineArgParser.IsUsageArgSpecified) { throw new ChoCommandLineArgUsageException(GetUsage(target)); } string cmdLineSwitch = null; if (commandLineArgObject == null || !commandLineArgObject.OnBeforeCommandLineArgObjectLoaded(commandLineArgs)) { MemberInfo[] memberInfos = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute)); MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoDefaultCommandLineArgAttribute)); ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; memberInfos = memberInfos.Concat(memberInfos1).ToArray(); if (memberInfos != null && memberInfos.Length > 0) { foreach (MemberInfo memberInfo in memberInfos) { ChoCommandLineArgAttribute commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true); if (commandLineArgumentAttribute == null) { continue; } cmdLineSwitch = commandLineArgumentAttribute.CommandLineSwitch; bool isSwitchSpecified = IsSwitchSpecified(commandLineArgParser.Switches, cmdLineSwitch, commandLineParserSettings.IgnoreCase); exception = ExtractNPopulateValue(target, memberInfo, cmdLineSwitch, commandLineArgParser, commandLineArgObject, isSwitchSpecified); if (exception != null) { break; } } if (exception == null) { cmdLineSwitch = DefaultCmdLineSwitch; MemberInfo defaultMemberInfo = GetMemberForDefaultSwitch(memberInfos1); exception = ExtractNPopulateValue(target, defaultMemberInfo, DefaultCmdLineSwitch, commandLineArgParser, commandLineArgObject, false); } } } if (commandLineArgObject != null) { if (exception != null) { if (!commandLineArgObject.OnCommandLineArgObjectLoadError(commandLineArgs, exception)) { throw new ChoCommandLineArgException("Found exception while loading `{3}` command line argument. {0}{0}{2}{0}{0}{1}".FormatString( Environment.NewLine, GetUsage(target), exception.Message, cmdLineSwitch), exception); } } else { commandLineArgObject.OnAfterCommandLineArgObjectLoaded(commandLineArgs); } } } }
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); } } } }