Beispiel #1
0
 public ChoCommandLineArgObject()
 {
     _commandLineArgsObjectAttribute = GetType().GetCustomAttribute(typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute;
     if (_commandLineArgsObjectAttribute == null)
     {
         throw new ChoFatalApplicationException("Missing ChoCommandLineArgObjectAttribute defined for '{0}' type.".FormatString(GetType().Name));
     }
 }
        private static ChoObjectInfo ConstructObjectInfos(Type cmdLineObjType)
        {
            if (cmdLineObjType != null)
            {
                Dictionary <string, ChoObjectInfo> objDict = _objDict;
                string elementPath = cmdLineObjType.Name;

                if (elementPath.IsNullOrWhiteSpace())
                {
                    return(ChoObjectInfo.Default);
                }

                if (!objDict.ContainsKey(elementPath))
                {
                    lock (_padLock)
                    {
                        if (!objDict.ContainsKey(elementPath))
                        {
                            ChoCommandLineArgObjectAttribute commandLineArgObjectAttribute =
                                ChoType.GetAttribute <ChoCommandLineArgObjectAttribute>(cmdLineObjType);

                            if (commandLineArgObjectAttribute != null)
                            {
                                ChoObjectInfo objInfo = new ChoObjectInfo();
                                objInfo.ApplicationName      = commandLineArgObjectAttribute.ApplicationName;
                                objInfo.Copyright            = commandLineArgObjectAttribute.Copyright;
                                objInfo.Description          = new ChoCDATA(commandLineArgObjectAttribute.Description);
                                objInfo.Version              = commandLineArgObjectAttribute.Version;
                                objInfo.AdditionalInfo       = new ChoCDATA(commandLineArgObjectAttribute.AdditionalInfo);
                                objInfo.ShowUsageIfEmpty     = commandLineArgObjectAttribute.ShowUsageIfEmptyInternal;
                                objInfo.DisplayDefaultValue  = commandLineArgObjectAttribute.DisplayDefaultValueInternal;
                                objInfo.DoNotShowUsageDetail = commandLineArgObjectAttribute.DoNotShowUsageDetail;
                                SetMetaDataSection(cmdLineObjType, objInfo);

                                return(objInfo);
                            }
                        }
                    }
                }
            }

            return(ChoObjectInfo.Default);
        }
        internal static void LoadFromConfig(Type cmdLineObjType, ChoCommandLineArgObjectAttribute attr)
        {
            if (attr == null)
            {
                return;
            }

            ChoObjectInfo objInfo = GetObjectInfo(cmdLineObjType);

            if (objInfo != null)
            {
                if (!objInfo.ApplicationName.IsNullOrEmpty())
                {
                    attr.ApplicationName = objInfo.ApplicationName;
                }
                if (objInfo.AdditionalInfo != null && !objInfo.AdditionalInfo.Value.IsNullOrEmpty())
                {
                    attr.AdditionalInfo = objInfo.AdditionalInfo.Value;
                }
                if (!objInfo.Copyright.IsNullOrEmpty())
                {
                    attr.Copyright = objInfo.Copyright;
                }
                if (objInfo.Description != null && !objInfo.Description.Value.IsNullOrEmpty())
                {
                    attr.Description = objInfo.Description.Value;
                }
                attr.DoNotShowUsageDetail = objInfo.DoNotShowUsageDetail;
                if (objInfo.ShowUsageIfEmpty.HasValue)
                {
                    attr.ShowUsageIfEmptyInternal = objInfo.ShowUsageIfEmpty.Value;
                }
                if (objInfo.DisplayDefaultValue.HasValue)
                {
                    attr.DisplayDefaultValueInternal = objInfo.DisplayDefaultValue.Value;
                }
                if (!objInfo.Version.IsNullOrEmpty())
                {
                    attr.Version = objInfo.Version;
                }
            }
        }
Beispiel #4
0
        public ChoCommandLineArgObject()
        {
            _commandLineArgsObjectAttribute = ChoType.GetAttribute(GetType(), typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute;

            //Discover and Hook the event handlers
            if (BeforeCommandLineArgLoadedInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoPreviewCommandLineArgEventArgs>(ref BeforeCommandLineArgLoadedInternal, ChoType.GetMethods(GetType(), typeof(ChoBeforeCommandLineArgLoadedHandlerAttribute)), this);
                if (BeforeCommandLineArgLoadedInternal != null && BeforeCommandLineArgLoadedInternal.GetInvocationList().Length > 0)
                {
                    BeforeCommandLineArgLoaded += (EventHandler <ChoPreviewCommandLineArgEventArgs>)BeforeCommandLineArgLoadedInternal.GetInvocationList()[BeforeCommandLineArgLoadedInternal.GetInvocationList().Length - 1];
                }
            }
            if (AfterCommandLineArgLoadedInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoCommandLineArgEventArgs>(ref AfterCommandLineArgLoadedInternal, ChoType.GetMethods(GetType(), typeof(ChoAfterCommandLineArgLoadedHandlerAttribute)), this);
                if (AfterCommandLineArgLoadedInternal != null && AfterCommandLineArgLoadedInternal.GetInvocationList().Length > 0)
                {
                    AfterCommandLineArgLoaded += (EventHandler <ChoCommandLineArgEventArgs>)AfterCommandLineArgLoadedInternal.GetInvocationList()[AfterCommandLineArgLoadedInternal.GetInvocationList().Length - 1];
                }
            }
            if (CommandLineArgLoadErrorInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoCommandLineArgErrorEventArgs>(ref CommandLineArgLoadErrorInternal, ChoType.GetMethods(GetType(), typeof(ChoCommandLineArgLoadErrorHandlerAttribute)), this);
                if (CommandLineArgLoadErrorInternal != null && CommandLineArgLoadErrorInternal.GetInvocationList().Length > 0)
                {
                    CommandLineArgLoadError += (EventHandler <ChoCommandLineArgErrorEventArgs>)CommandLineArgLoadErrorInternal.GetInvocationList()[CommandLineArgLoadErrorInternal.GetInvocationList().Length - 1];
                }
            }

            if (BeforeCommandLineArgObjectLoadedInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoPreviewCommandLineArgObjectEventArgs>(ref BeforeCommandLineArgObjectLoadedInternal, ChoType.GetMethods(GetType(), typeof(ChoBeforeCommandLineArgObjectLoadedHandlerAttribute)), this);
                if (BeforeCommandLineArgObjectLoadedInternal != null && BeforeCommandLineArgObjectLoadedInternal.GetInvocationList().Length > 0)
                {
                    BeforeCommandLineArgObjectLoaded += (EventHandler <ChoPreviewCommandLineArgObjectEventArgs>)BeforeCommandLineArgObjectLoadedInternal.GetInvocationList()[BeforeCommandLineArgObjectLoadedInternal.GetInvocationList().Length - 1];
                }
            }

            if (AfterCommandLineArgObjectLoadedInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoCommandLineArgObjectEventArgs>(ref AfterCommandLineArgObjectLoadedInternal, ChoType.GetMethods(GetType(), typeof(ChoAfterCommandLineArgObjectLoadedHandlerAttribute)), this);
                if (AfterCommandLineArgObjectLoadedInternal != null && AfterCommandLineArgObjectLoadedInternal.GetInvocationList().Length > 0)
                {
                    AfterCommandLineArgObjectLoaded += (EventHandler <ChoCommandLineArgObjectEventArgs>)AfterCommandLineArgObjectLoadedInternal.GetInvocationList()[AfterCommandLineArgObjectLoadedInternal.GetInvocationList().Length - 1];
                }
            }

            if (CommandLineArgObjectLoadErrorInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoCommandLineArgObjectErrorEventArgs>(ref CommandLineArgObjectLoadErrorInternal, ChoType.GetMethods(GetType(), typeof(ChoCommandLineArgObjectLoadErrorHandlerAttribute)), this);
                if (CommandLineArgObjectLoadErrorInternal != null && CommandLineArgObjectLoadErrorInternal.GetInvocationList().Length > 0)
                {
                    CommandLineArgObjectLoadError += (EventHandler <ChoCommandLineArgObjectErrorEventArgs>)CommandLineArgObjectLoadErrorInternal.GetInvocationList()[CommandLineArgObjectLoadErrorInternal.GetInvocationList().Length - 1];
                }
            }

            if (CommandLineArgMemberNotFoundInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoCommandLineArgNotFoundEventArgs>(ref CommandLineArgMemberNotFoundInternal, ChoType.GetMethods(GetType(), typeof(ChoCommandLineArgMemberNotFoundHandlerAttribute)), this);
                if (CommandLineArgMemberNotFoundInternal != null && CommandLineArgMemberNotFoundInternal.GetInvocationList().Length > 0)
                {
                    CommandLineArgMemberNotFound += (EventHandler <ChoCommandLineArgNotFoundEventArgs>)CommandLineArgMemberNotFoundInternal.GetInvocationList()[CommandLineArgMemberNotFoundInternal.GetInvocationList().Length - 1];
                }
            }
            if (UnrecognizedCommandLineArgFoundInternal == null)
            {
                EventHandlerEx.LoadHandlers <ChoUnrecognizedCommandLineArgEventArg>(ref UnrecognizedCommandLineArgFoundInternal, ChoType.GetMethods(GetType(), typeof(ChoUnrecognizedCommandLineArgFoundHandlerAttribute)), this);
                if (UnrecognizedCommandLineArgFoundInternal != null && UnrecognizedCommandLineArgFoundInternal.GetInvocationList().Length > 0)
                {
                    UnrecognizedCommandLineArgFound += (EventHandler <ChoUnrecognizedCommandLineArgEventArg>)UnrecognizedCommandLineArgFoundInternal.GetInvocationList()[UnrecognizedCommandLineArgFoundInternal.GetInvocationList().Length - 1];
                }
            }
        }
        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;

            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);
                    }
                }
            }
        }
        private static bool HasUsageSwitchSpecified(ChoCommandLineArgParser commandLineArgParser, ChoCommandLineArgObjectAttribute commandLineArgumentsObjectAttribute)
        {
            if (commandLineArgumentsObjectAttribute == null ||
                commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace())
            {
                return(false);
            }

            foreach (string usageSwitch in commandLineArgumentsObjectAttribute.UsageSwitch.SplitNTrim())
            {
                if (commandLineArgParser.Contains(usageSwitch))
                {
                    return(true);
                }
            }

            return(false);
        }