Exemple #1
0
        private string GetDefaultValueText(MemberInfo memberInfo)
        {
            string name         = null;
            string defaultValue = null;
            bool   isDefaultValueSpecified;
            ChoCommandLineArgAttribute defaultCommandLineArgAttribute = null;

            defaultCommandLineArgAttribute = (ChoCommandLineArgAttribute)ChoType.GetMemberAttribute(memberInfo, typeof(ChoCommandLineArgAttribute));
            if (defaultCommandLineArgAttribute == null)
            {
                return(null);
            }

            name = ChoType.GetMemberName(memberInfo);
            if (ChoType.GetMemberType(memberInfo) != typeof(bool))
            {
                isDefaultValueSpecified = ChoCmdLineArgMetaDataManager.TryGetDefaultValue(this, name, defaultCommandLineArgAttribute, out defaultValue);
                if (isDefaultValueSpecified)
                {
                    return(defaultValue);
                }
            }

            return(null);
        }
Exemple #2
0
        private string GetCmdLineSwitches(char cmdLineSwitchChar, ChoCommandLineArgAttribute commandLineArgumentAttribute)
        {
            if (commandLineArgumentAttribute.Aliases.IsNullOrWhiteSpace())
            {
                return("{0}{1}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch));
            }

            StringBuilder msg = new StringBuilder("{0}{1}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch));

            foreach (string alias in commandLineArgumentAttribute.Aliases.SplitNTrim())
            {
                msg.Append(" {0}{1}".FormatString(cmdLineSwitchChar, alias));
            }

            return(msg.ToString());
        }
        private static MemberInfo GetMemberForSwitch(MemberInfo[] memberInfos, string switchString, bool ignoreCase)
        {
            if (memberInfos.IsNullOrEmpty())
            {
                return(null);
            }

            foreach (MemberInfo memberInfo in memberInfos)
            {
                ChoCommandLineArgAttribute commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);

                if (commandLineArgumentAttribute == null)
                {
                    continue;
                }

                if (String.Compare(commandLineArgumentAttribute.CommandLineSwitch, switchString, ignoreCase) == 0)
                {
                    return(memberInfo);
                }
            }

            return(null);
        }
        private static string GetCmdLineSwitch(MemberInfo memberInfo)
        {
            string cmdLineSwitch = null;
            ChoCommandLineArgAttribute commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);

            if (commandLineArgumentAttribute != null)
            {
                cmdLineSwitch = commandLineArgumentAttribute.CommandLineSwitch;
            }
            else
            {
                ChoPositionalCommandLineArgAttribute posCommandLineArgumentAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true);
                if (posCommandLineArgumentAttribute != null)
                {
                    cmdLineSwitch = posCommandLineArgumentAttribute.ShortName;
                }
                if (cmdLineSwitch.IsNullOrWhiteSpace())
                {
                    cmdLineSwitch = posCommandLineArgumentAttribute.Position.ToString();
                }
            }

            return(cmdLineSwitch);
        }
Exemple #5
0
        public virtual string GetUsage()
        {
            Type type = GetType();

            StringBuilder builder      = new StringBuilder();
            StringBuilder whereBuilder = new StringBuilder();

            ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me;
            char cmdLineValueSeparator = commandLineParserSettings.ValueSeparators != null && commandLineParserSettings.ValueSeparators.Length > 0 ? commandLineParserSettings.ValueSeparators[0] : ':';
            char cmdLineSwitchChar     = commandLineParserSettings.SwitchChars != null && commandLineParserSettings.SwitchChars.Length > 0 ? commandLineParserSettings.SwitchChars[0] : '-';

            builder.Append(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location).ToUpper());
            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(type);

            if (memberInfos != null && memberInfos.Length > 0)
            {
                ChoCommandLineArgAttribute           commandLineArgumentAttribute   = null;
                ChoDefaultCommandLineArgAttribute    defaultCommandLineArgAttribute = null;
                ChoPositionalCommandLineArgAttribute posCommandLineArgAttribute     = null;

                List <Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> > memberList = new List <Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> >();
                foreach (MemberInfo memberInfo in memberInfos)
                {
                    commandLineArgumentAttribute   = null;
                    defaultCommandLineArgAttribute = null;

                    defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                    if (commandLineArgumentAttribute == null)
                    {
                        defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true);
                        if (posCommandLineArgAttribute == null)
                        {
                            continue;
                        }
                    }

                    memberList.Add(new Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo>(defaultCommandLineArgAttribute, memberInfo));
                }

                bool isEmptyShortName;

                memberList.Sort((x, y) =>
                                x.Item1.Order.CompareTo(y.Item1.Order));

                MemberInfo memberInfo1 = null;
                foreach (Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> tuple in memberList)
                {
                    memberInfo1 = tuple.Item2;

                    commandLineArgumentAttribute   = null;
                    defaultCommandLineArgAttribute = null;

                    defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo1.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                    if (commandLineArgumentAttribute == null)
                    {
                        defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo1.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true);
                        if (posCommandLineArgAttribute == null)
                        {
                            continue;
                        }
                    }

                    isEmptyShortName = !defaultCommandLineArgAttribute.ShortName.IsNull() &&
                                       defaultCommandLineArgAttribute.ShortName.Length == 0;

                    if (!defaultCommandLineArgAttribute.IsRequired)
                    {
                        builder.Append(" [");
                    }
                    else
                    {
                        builder.Append(" ");
                    }

                    Type memberType = ChoType.GetMemberType(memberInfo1);
                    if (memberType.IsNullableType())
                    {
                        memberType = Nullable.GetUnderlyingType(memberType);
                    }

                    if (commandLineArgumentAttribute != null)
                    {
                        builder.Append("{0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeparator.ToString() : String.Empty));
                    }

                    string description = null;
                    if (commandLineArgumentAttribute != null)
                    {
                        description = commandLineArgumentAttribute.Description;
                    }
                    else if (posCommandLineArgAttribute != null)
                    {
                        description = posCommandLineArgAttribute.Description;
                    }
                    else
                    {
                        description = defaultCommandLineArgAttribute.Description;
                    }

                    if (_commandLineArgsObjectAttribute.GetDisplayDefaultValue())
                    {
                        string defaultValue = null;
                        defaultValue = GetDefaultValueText(memberInfo1);
                        if (defaultValue != null && memberType != typeof(bool))
                        {
                            description = "{0} [DEFAULT: {1}]".FormatString(description, defaultValue);
                        }
                    }

                    if (commandLineArgumentAttribute != null)
                    {
                        whereBuilder.AppendFormat("{1}{3}{2}{0}", Environment.NewLine, GetCmdLineSwitches(cmdLineSwitchChar, commandLineArgumentAttribute),
                                                  description.WrapLongLines(commandLineArgumentAttribute.DescriptionFormatLineSize, String.Empty,
                                                                            commandLineArgumentAttribute.DescriptionFormatLineBreakChar, commandLineArgumentAttribute.NoOfTabsSwitchDescFormatSeparator),
                                                  "\t".Repeat(commandLineArgumentAttribute.NoOfTabsSwitchDescFormatSeparator));
                    }
                    else if (posCommandLineArgAttribute != null)
                    {
                        whereBuilder.AppendFormat("{1}{3}{2}{0}", Environment.NewLine,
                                                  defaultCommandLineArgAttribute.ShortName.IsNull() ? "Position{0}".FormatString(posCommandLineArgAttribute.Position) : defaultCommandLineArgAttribute.ShortName,
                                                  description.WrapLongLines(posCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty,
                                                                            posCommandLineArgAttribute.DescriptionFormatLineBreakChar, posCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator),
                                                  "\t".Repeat(posCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator));
                    }
                    else
                    {
                        whereBuilder.AppendFormat("{3}{2}{1}{0}", Environment.NewLine,
                                                  description.WrapLongLines(defaultCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty,
                                                                            defaultCommandLineArgAttribute.DescriptionFormatLineBreakChar, defaultCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator),
                                                  "\t".Repeat(defaultCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator), DefaultCmdLineSwitch);
                    }

                    if (memberType == typeof(int))
                    {
                        if (!defaultCommandLineArgAttribute.ShortName.IsNull())
                        {
                            builder.Append(defaultCommandLineArgAttribute.ShortName);
                        }
                        else
                        {
                            builder.Append("<int>");
                        }
                    }
                    else if (memberType == typeof(uint))
                    {
                        if (!defaultCommandLineArgAttribute.ShortName.IsNull())
                        {
                            builder.Append(defaultCommandLineArgAttribute.ShortName);
                        }
                        else
                        {
                            builder.Append("<uint>");
                        }
                    }
                    else if (memberType == typeof(bool))
                    {
                        builder.Remove(builder.Length - 1, 1);
                    }
                    else if (memberType == typeof(string))
                    {
                        if (!defaultCommandLineArgAttribute.ShortName.IsNull())
                        {
                            builder.Append(defaultCommandLineArgAttribute.ShortName);
                        }
                        else
                        {
                            builder.Append("<string>");
                        }
                    }
                    else if (memberType.IsEnum)
                    {
                        //builder.Append("{0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeparator.ToString() : String.Empty));
                        builder.Append("{");
                        bool first = true;
                        foreach (FieldInfo field in memberType.GetFields())
                        {
                            if (field.IsStatic)
                            {
                                if (first)
                                {
                                    first = false;
                                }
                                else
                                {
                                    builder.Append(" | ");
                                }

                                builder.Append(field.Name);
                            }
                        }
                        builder.Append('}');
                    }
                    else
                    {
                        if (!defaultCommandLineArgAttribute.ShortName.IsNull())
                        {
                            builder.Append(defaultCommandLineArgAttribute.ShortName);
                        }
                        else
                        {
                            builder.Append("<Unknown>");
                        }
                    }

                    if (!defaultCommandLineArgAttribute.IsRequired)
                    {
                        builder.Append("]");
                    }
                }
            }

            if (!_commandLineArgsObjectAttribute.DoNotShowUsageDetail)
            {
                builder.Append(Environment.NewLine);
                builder.Append(Environment.NewLine);
                builder.Append(whereBuilder.ToString().Indent());
                builder.Append(Environment.NewLine);

                foreach (ChoCommandLineArgAdditionalUsageAttribute commandLineArgAdditionalUsageAttribute in ChoType.GetAttributes <ChoCommandLineArgAdditionalUsageAttribute>(type))
                {
                    if (commandLineArgAdditionalUsageAttribute != null && !commandLineArgAdditionalUsageAttribute.AdditionalUsageText.IsNull())
                    {
                        builder.Append(commandLineArgAdditionalUsageAttribute.AdditionalUsageText);

                        builder.Append(Environment.NewLine);
                        builder.Append(Environment.NewLine);
                    }
                }
            }
            return(builder.ToString());
        }
        public static string GetUsage(object target)
        {
            ChoGuard.ArgumentNotNull(target, "Target");

            StringBuilder builder      = new StringBuilder();
            StringBuilder whereBuilder = new StringBuilder();

            ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me;
            char cmdLineValueSeperator = commandLineParserSettings.ValueSeperators != null && commandLineParserSettings.ValueSeperators.Length > 0 ? commandLineParserSettings.ValueSeperators[0] : ':';
            char cmdLineSwitchChar     = commandLineParserSettings.SwitchChars != null && commandLineParserSettings.SwitchChars.Length > 0 ? commandLineParserSettings.SwitchChars[0] : '-';

            builder.Append(ChoApplication.EntryAssemblyFileName);
            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(target.GetType());

            if (memberInfos != null && memberInfos.Length > 0)
            {
                ChoCommandLineArgAttribute        commandLineArgumentAttribute   = null;
                ChoDefaultCommandLineArgAttribute defaultCommandLineArgAttribute = null;
                bool isEmptyShortName;

                foreach (MemberInfo memberInfo in memberInfos)
                {
                    //if (ChoType.IsReadOnlyMember(memberInfo))
                    //    continue;

                    commandLineArgumentAttribute   = null;
                    defaultCommandLineArgAttribute = null;

                    commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                    if (commandLineArgumentAttribute == null)
                    {
                        defaultCommandLineArgAttribute = (ChoDefaultCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoDefaultCommandLineArgAttribute>(true);
                        if (defaultCommandLineArgAttribute == null)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        defaultCommandLineArgAttribute = commandLineArgumentAttribute;
                    }

                    isEmptyShortName = !defaultCommandLineArgAttribute.ShortName.IsNull() &&
                                       defaultCommandLineArgAttribute.ShortName.Length == 0;

                    if (commandLineArgumentAttribute != null)
                    {
                        builder.Append(" {0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeperator.ToString() : String.Empty));
                    }
                    else
                    {
                        builder.Append(" ");
                    }

                    Type memberType = ChoType.GetMemberType(memberInfo);

                    if (defaultCommandLineArgAttribute.IsRequired)
                    {
                        if (!isEmptyShortName)
                        {
                            builder.Append("[");
                        }
                    }

                    if (commandLineArgumentAttribute != null)
                    {
                        whereBuilder.AppendFormat("{1}{2}{4}{3}{0}", Environment.NewLine, cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch,
                                                  commandLineArgumentAttribute.Description.WrapLongLines(commandLineArgumentAttribute.DescriptionFormatLineSize, String.Empty,
                                                                                                         commandLineArgumentAttribute.DescriptionFormatLineBreakChar, commandLineArgumentAttribute.DescriptionFormatLineNoOfTabs),
                                                  commandLineArgumentAttribute.SwitchValueSeperator);
                    }
                    else
                    {
                        whereBuilder.AppendFormat("{3}{2}{1}{0}", Environment.NewLine,
                                                  defaultCommandLineArgAttribute.Description.WrapLongLines(defaultCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty,
                                                                                                           defaultCommandLineArgAttribute.DescriptionFormatLineBreakChar, defaultCommandLineArgAttribute.DescriptionFormatLineNoOfTabs),
                                                  defaultCommandLineArgAttribute.SwitchValueSeperator, DefaultCmdLineSwitch);
                    }

                    if (!defaultCommandLineArgAttribute.ShortName.IsNull())
                    {
                        builder.Append(defaultCommandLineArgAttribute.ShortName);
                    }
                    else
                    {
                        if (memberType == typeof(int))
                        {
                            builder.Append("<int>");
                        }
                        else if (memberType == typeof(uint))
                        {
                            builder.Append("<uint>");
                        }
                        else if (memberType == typeof(bool))
                        {
                            builder.Append("{True|False}");
                        }
                        else if (memberType == typeof(string))
                        {
                            builder.Append("<string>");
                        }
                        else if (memberType.IsEnum)
                        {
                            builder.Append("{");
                            bool first = true;
                            foreach (FieldInfo field in memberType.GetFields())
                            {
                                if (field.IsStatic)
                                {
                                    if (first)
                                    {
                                        first = false;
                                    }
                                    else
                                    {
                                        builder.Append('|');
                                    }
                                    builder.Append(field.Name);
                                }
                            }
                            builder.Append('}');
                        }
                        else
                        {
                            builder.Append("<Unknown>");
                        }
                    }
                    if (defaultCommandLineArgAttribute.IsRequired)
                    {
                        if (!isEmptyShortName)
                        {
                            builder.Append("]");
                        }
                    }
                }
            }

            builder.Append(Environment.NewLine);
            builder.Append(Environment.NewLine);
            builder.Append("Where");
            builder.Append(Environment.NewLine);
            builder.Append(whereBuilder.ToString().Indent());
            builder.Append(Environment.NewLine);

            if (target is ChoCommandLineArgObject)
            {
                string additionalUsageText = ((ChoCommandLineArgObject)target).AdditionalUsageText;
                if (!additionalUsageText.IsNullOrWhiteSpace())
                {
                    builder.Append(additionalUsageText);

                    builder.Append(Environment.NewLine);
                    builder.Append(Environment.NewLine);
                }
            }

            return(builder.ToString());
        }
        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);
                    }
                }
            }
        }
        private static Exception ExtractNPopulateValue(object target, MemberInfo memberInfo, string switchString, ChoCommandLineArgParser commandLineArgParser,
                                                       ChoCommandLineArgObject commandLineArgObject, bool isSwitchSpecified)
        {
            ChoCommandLineArgAttribute        commandLineArgumentAttribute   = null;
            ChoDefaultCommandLineArgAttribute defaultCommandLineArgAttribute = null;

            if (memberInfo == null)
            {
                if (commandLineArgObject != null)
                {
                    commandLineArgObject.OnCommandLineArgMemberNotFound(switchString, commandLineArgParser[switchString]);
                }
            }
            else
            {
                //if (ChoType.IsReadOnlyMember(memberInfo))
                //    return null;

                commandLineArgumentAttribute   = null;
                defaultCommandLineArgAttribute = null;

                commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                if (commandLineArgumentAttribute == null)
                {
                    defaultCommandLineArgAttribute = (ChoDefaultCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoDefaultCommandLineArgAttribute>(true);
                    if (defaultCommandLineArgAttribute == null)
                    {
                        return(null);
                    }
                }

                string cmdLineArgValue    = null;
                object newCmdLineArgValue = null;

                try
                {
                    if (defaultCommandLineArgAttribute != null)
                    {
                        cmdLineArgValue = commandLineArgParser.DefaultArgs.Length > 0 ? commandLineArgParser.DefaultArgs[0] : null;
                    }
                    else if (commandLineArgumentAttribute != null)
                    {
                        cmdLineArgValue = commandLineArgParser[commandLineArgumentAttribute.CommandLineSwitch];
                        defaultCommandLineArgAttribute = commandLineArgumentAttribute;
                    }
                    else
                    {
                        return(null);
                    }

                    object defaultCmdLineArgValue = defaultCommandLineArgAttribute.DefaultValue;

                    if (isSwitchSpecified && cmdLineArgValue == null && defaultCommandLineArgAttribute.FallbackValue != null)
                    {
                        defaultCmdLineArgValue = defaultCommandLineArgAttribute.FallbackValue;
                    }

                    if (commandLineArgObject == null || !commandLineArgObject.OnBeforeCommandLineArgLoaded(memberInfo.Name, cmdLineArgValue, defaultCommandLineArgAttribute.DefaultValue, defaultCommandLineArgAttribute.FallbackValue))
                    {
                        if (!cmdLineArgValue.IsNullOrWhiteSpace())
                        {
                            newCmdLineArgValue = ChoConvert.ConvertFrom(target, ChoString.ExpandPropertiesEx(cmdLineArgValue),
                                                                        ChoType.GetMemberType(memberInfo),
                                                                        ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        }
                        else if (defaultCmdLineArgValue != null)
                        {
                            if (defaultCmdLineArgValue is string)
                            {
                                newCmdLineArgValue = ChoConvert.ConvertFrom(target, ChoString.ExpandPropertiesEx(defaultCmdLineArgValue as string),
                                                                            ChoType.GetMemberType(memberInfo),
                                                                            ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                            }
                            else
                            {
                                newCmdLineArgValue = ChoConvert.ConvertFrom(target, defaultCmdLineArgValue, ChoType.GetMemberType(memberInfo),
                                                                            ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                            }
                        }

                        if (newCmdLineArgValue != null)
                        {
                            ChoType.SetMemberValue(target, memberInfo, newCmdLineArgValue);
                            if (commandLineArgObject != null)
                            {
                                commandLineArgObject.OnAfterCommandLineArgLoaded(memberInfo.Name, newCmdLineArgValue);
                            }
                        }
                        else if (defaultCommandLineArgAttribute.IsRequired)
                        {
                            throw new ChoCommandLineArgException("Missing arg value for '{0}' required command line switch.".FormatString(
                                                                     commandLineArgumentAttribute == null ? DefaultCmdLineSwitch : commandLineArgumentAttribute.CommandLineSwitch));
                        }
                    }
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (commandLineArgObject != null && commandLineArgObject.OnCommandLineArgLoadError(memberInfo.Name, cmdLineArgValue, ex))
                    {
                    }
                    else
                    {
                        return(ex);
                    }
                }
            }

            return(null);
        }
        //private static void AssignToDefaultValues(ChoCommandLineArgObject commandLineArgObject)
        //{
        //    object newCmdLineArgValue = null;

        //    string name = null;
        //    string defaultValue = null;
        //    bool isDefaultValueSpecified;
        //    MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(commandLineArgObject.GetType());
        //    if (memberInfos != null && memberInfos.Length > 0)
        //    {
        //        ChoCommandLineArgAttribute defaultCommandLineArgAttribute = null;
        //        foreach (MemberInfo memberInfo in memberInfos)
        //        {
        //            defaultCommandLineArgAttribute = (ChoCommandLineArgAttribute)ChoType.GetMemberAttribute(memberInfo, typeof(ChoCommandLineArgAttribute));
        //            if (defaultCommandLineArgAttribute == null) continue;

        //            name = ChoType.GetMemberName(memberInfo);
        //            defaultValue = null;
        //            if (ChoType.GetMemberType(memberInfo) == typeof(bool))
        //                continue;

        //            isDefaultValueSpecified = ChoCmdLineArgMetaDataManager.TryGetDefaultValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out defaultValue);
        //            if (!isDefaultValueSpecified)
        //                continue;
        //            try
        //            {
        //                defaultValue = ChoString.ExpandPropertiesEx(defaultValue);
        //                object newConvertedValue = ChoConvert.ConvertFrom(defaultValue, memberInfo, commandLineArgObject);

        //                //object newConvertedValue = ChoConvert.ConvertFrom(commandLineArgObject, defaultValue, ChoType.GetMemberType(memberInfo),
        //                //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
        //                ChoType.SetMemberValue(commandLineArgObject, memberInfo, newCmdLineArgValue);
        //            }
        //            catch //(Exception ex)
        //            {
        //            }
        //        }
        //    }
        //}

        private static Exception ExtractNPopulateValue(ChoCommandLineArgObject commandLineArgObject, MemberInfo memberInfo, ChoCommandLineArgParser commandLineArgParser)
        {
            ChoDefaultCommandLineArgAttribute    defaultCommandLineArgAttribute = null;
            ChoCommandLineArgAttribute           commandLineArgumentAttribute   = null;
            ChoPositionalCommandLineArgAttribute posCommandLineArgAttribute     = null;

            if (ChoType.IsReadOnlyMember(memberInfo))
            {
                return(null);
            }

            commandLineArgumentAttribute = null;
            posCommandLineArgAttribute   = null;

            defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
            if (commandLineArgumentAttribute == null)
            {
                defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true);
                if (posCommandLineArgAttribute == null)
                {
                    return(null);
                }
            }

            bool   containsCmdLineArg       = false;
            string cmdLineArgValue          = null;
            object newCmdLineArgValue       = null;
            string defaultValue             = null;
            bool   isDefaultValueSpecified  = false;
            bool   isFallbackValueSpecified = false;
            string name             = null;
            string fallbackValue    = null;
            object fallbackValueObj = null;
            object defaultValueObj  = null;

            name = ChoType.GetMemberName(memberInfo);

            try
            {
                if (posCommandLineArgAttribute != null)
                {
                    if (!commandLineArgParser.IsSwitchSpecified(posCommandLineArgAttribute.Position))
                    {
                        commandLineArgObject.RaiseCommandLineArgNotFound(posCommandLineArgAttribute.Position.ToString(), ref cmdLineArgValue);
                    }
                    cmdLineArgValue = commandLineArgParser[posCommandLineArgAttribute.Position];
                }
                else if (commandLineArgumentAttribute != null)
                {
                    if (!commandLineArgParser.IsSwitchSpecified(commandLineArgumentAttribute.CommandLineSwitch))
                    {
                        commandLineArgObject.RaiseCommandLineArgNotFound(commandLineArgumentAttribute.CommandLineSwitch, ref cmdLineArgValue);
                    }

                    if (ChoType.GetMemberType(memberInfo) == typeof(bool))
                    {
                        containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                        if (containsCmdLineArg)
                        {
                            cmdLineArgValue = "True";
                            //cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                            //if (cmdLineArgValue.IsNullOrWhiteSpace())
                            //    cmdLineArgValue = "True";
                        }
                        else
                        {
                            containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, "{0}-".FormatString(commandLineArgumentAttribute.CommandLineSwitch), commandLineArgumentAttribute.Aliases);
                            if (containsCmdLineArg)
                            {
                                cmdLineArgValue = "False";
                            }
                        }
                    }
                    //else if (ChoType.GetMemberType(memberInfo).IsEnum)
                    //{
                    //    containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, Enum.GetNames(ChoType.GetMemberType(memberInfo)));
                    //    if (containsCmdLineArg)
                    //        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, Enum.GetNames(ChoType.GetMemberType(memberInfo)));
                    //    else
                    //        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                    //}
                    else
                    {
                        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                    }
                }
                else
                {
                    return(null);
                }

                //if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                //{
                isDefaultValueSpecified  = ChoCmdLineArgMetaDataManager.TryGetDefaultValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out defaultValue);
                isFallbackValueSpecified = ChoCmdLineArgMetaDataManager.TryGetFallbackValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out fallbackValue);
                //}

                try
                {
                    if (isFallbackValueSpecified)
                    {
                        //                    fallbackValueObj = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(fallbackValue),
                        //ChoType.GetMemberType(memberInfo),
                        //ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));

                        fallbackValueObj = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(fallbackValue), memberInfo, commandLineArgObject);
                    }
                }
                catch
                {
                }

                try
                {
                    if (isDefaultValueSpecified)
                    {
                        //defaultValueObj = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(defaultValue), ChoType.GetMemberType(memberInfo),
                        //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        defaultValueObj = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(defaultValue), memberInfo, commandLineArgObject);
                    }
                }
                catch
                {
                }

                if (commandLineArgObject != null && !commandLineArgObject.RaiseBeforeCommandLineArgLoaded(memberInfo.Name, ref cmdLineArgValue, defaultValueObj, fallbackValueObj))
                {
                    if (!cmdLineArgValue.IsNull())
                    {
                        newCmdLineArgValue = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(cmdLineArgValue), memberInfo, commandLineArgObject);

                        //newCmdLineArgValue = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(cmdLineArgValue),
                        //    ChoType.GetMemberType(memberInfo),
                        //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                    }

                    if (newCmdLineArgValue == null && defaultCommandLineArgAttribute.IsRequired)
                    {
                        if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                        {
                            if (commandLineArgumentAttribute != null)
                            {
                                throw new ChoCommandLineArgException("Missing arg value for '{0}' required command line switch.".FormatString(
                                                                         commandLineArgumentAttribute == null ? ChoCommandLineArgObject.DefaultCmdLineSwitch : commandLineArgumentAttribute.CommandLineSwitch),
                                                                     commandLineArgObject.GetUsage());
                            }
                            else if (posCommandLineArgAttribute != null)
                            {
                                if (posCommandLineArgAttribute.ShortName.IsNull())
                                {
                                    throw new ChoCommandLineArgException("Missing positional arg value at '{0}' position.".FormatString(
                                                                             posCommandLineArgAttribute == null ? ChoCommandLineArgObject.DefaultCmdLineSwitch : posCommandLineArgAttribute.Position.ToString()), commandLineArgObject.GetUsage());
                                }
                                else
                                {
                                    throw new ChoCommandLineArgException("Missing '{0}' argument.".FormatString(posCommandLineArgAttribute.ShortName), commandLineArgObject.GetUsage());
                                }
                            }
                            else
                            {
                                throw new ChoCommandLineArgException("Missing arg value at '{0}' position.".FormatString(ChoCommandLineArgObject.DefaultCmdLineSwitch), commandLineArgObject.GetUsage());
                            }
                        }
                    }
                    else
                    {
                        if (newCmdLineArgValue == null)
                        {
                            if (isDefaultValueSpecified)
                            {
                                //if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                                newCmdLineArgValue = defaultValueObj;
                                //else
                                //    newCmdLineArgValue = false;
                            }
                        }

                        ChoType.SetMemberValue(commandLineArgObject, memberInfo, newCmdLineArgValue);
                        if (commandLineArgObject != null)
                        {
                            commandLineArgObject.RaiseAfterCommandLineArgLoaded(memberInfo.Name, newCmdLineArgValue);
                        }
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (commandLineArgObject != null && commandLineArgObject.RaiseCommandLineArgLoadError(memberInfo.Name, cmdLineArgValue, ex))
                {
                }
                else
                {
                    if (defaultCommandLineArgAttribute.IsRequired)
                    {
                        return(ex);
                    }

                    if (fallbackValueObj != null)
                    {
                        ChoType.SetMemberValue(commandLineArgObject, memberInfo, fallbackValueObj);
                    }
                    else
                    {
                        return(ex);
                    }
                }
            }
            return(null);
        }