Exemplo n.º 1
0
        private IList <OptInfo> CollectOptInfos(HelpCategory category)
        {
            var options = new List <OptInfo>();

            foreach (var info in GetMemberInfos())
            {
                string name;
                var    shortArgs = info.GetAttributes <ShortArgumentAttribute>();
                var    longArgs  = info.GetAttributes <ArgumentAttribute>();
                if (longArgs.Length == 0)
                {
                    continue;
                }

                var longArg = longArgs[0];
                if (longArg.Category > category)
                {
                    continue;
                }

                string longName = string.IsNullOrEmpty(longArg.Arg) ? info.Name : longArg.Arg;
                if (opts.CaseType == ArgumentCaseType.Insensitive ||
                    opts.CaseType == ArgumentCaseType.OnlyLower)
                {
                    longName = longName.ToLower();
                }

                name = shortArgs.Length != 0 ? shortArgs[0].Arg : longName;

                GetMemberType(info);

                string helpVar = longArgs[0].HelpVar?.ToUpper();
                if (string.IsNullOrEmpty(helpVar))
                {
                    Type etype = longs[longName].ElementType;
                    helpVar = etype.IsEnum ? FormatEnum(etype) : etype.Name.ToUpper();
                }

                var arg     = longArgs[0];
                var handler = longs[longName];
                // XXX: need to implement multi args
                var optInfo = new OptInfo(name, handler.IsFlag, arg.HelpText, helpVar, opts.UsagePrefix, false);
                optInfo.Longs.Add(longName);

                if (shortArgs.Length != 0)
                {
                    optInfo.Shorts.Add(shortArgs[0].Arg);
                }

                if (opts.UsageShowAliases == UsageAliasShowOption.Show)
                {
                    var aliases = info.GetAttributes <ArgumentAliasAttribute>();
                    foreach (var alias in aliases)
                    {
                        string an = alias.Alias;
                        if (opts.CaseType == ArgumentCaseType.Insensitive ||
                            opts.CaseType == ArgumentCaseType.OnlyLower)
                        {
                            an = an.ToLower();
                        }

                        optInfo.Longs.Add(an);
                    }

                    var shortAliases = info.GetAttributes <ShortArgumentAliasAttribute>();
                    foreach (var alias in shortAliases)
                    {
                        optInfo.Shorts.Add(alias.Alias);
                    }
                }

                options.Add(optInfo);
            }

            //options.Sort();
            return(options);
        }
Exemplo n.º 2
0
        private List <OptInfo> CollectOptInfos(Type me, BindingFlags flags)
        {
            var options = new List <OptInfo>();


            foreach (MemberInfo[] infoArray in new MemberInfo[][] { me.GetFields(flags), me.GetProperties(flags) })
            {
                foreach (MemberInfo info in infoArray)
                {
                    string name;
                    var    sa = info.GetCustomAttributes(typeof(ShortArgument), true) as ShortArgument[];
                    var    la = info.GetCustomAttributes(typeof(Argument), true) as Argument[];
                    if (la.Length == 0)
                    {
                        continue;
                    }
                    var longName = String.IsNullOrEmpty(la[0].Arg) ? info.Name : la[0].Arg;
                    if (opts.CaseType == ArgumentCaseType.Insensitive || opts.CaseType == ArgumentCaseType.OnlyLower)
                    {
                        longName = longName.ToLower();
                    }
                    if (sa.Length != 0)
                    {
                        name = new string(sa[0].Arg, 1);
                    }
                    else
                    {
                        name = longName;
                    }
                    GetMemberType(info);

                    var hv = la[0].HelpVar;
                    if (String.IsNullOrEmpty(hv))
                    {
                        hv = longs[longName].ElementType.Name;
                    }

                    var arg     = la[0];
                    var handler = longs[longName];
                    var oi      = new OptInfo(name, handler.IsFlag, arg.HelpText, hv.ToUpper(), opts.UsagePrefix);
                    oi.Longs.Add(longName);

                    if (sa.Length != 0)
                    {
                        oi.Shorts.Add(new string(sa[0].Arg, 1));
                    }
                    if (opts.UsageShowAliases == UsageAliasShowOption.Show)
                    {
                        foreach (ArgumentAlias alias in info.GetCustomAttributes(typeof(ArgumentAlias), true))
                        {
                            var an = alias.Alias;
                            if (opts.CaseType == ArgumentCaseType.Insensitive || opts.CaseType == ArgumentCaseType.OnlyLower)
                            {
                                an = an.ToLower();
                            }
                            oi.Longs.Add(an);
                        }
                        foreach (ShortArgumentAlias alias in info.GetCustomAttributes(typeof(ShortArgumentAlias), true))
                        {
                            oi.Shorts.Add(new string(alias.Alias, 1));
                        }
                    }
                    options.Add(oi);
                }
            }

            options.Sort();
            return(options);
        }