示例#1
0
 public CommandLineParserTypeMetaInfo(OptGetter opt, Dictionary <char, PropertyInfo> shortOptionTargets, Dictionary <string, PropertyInfo> longOptionTargets, IList <MethodInfo> errorHandlers)
 {
     Opt = opt;
     ShortOptionTargets = shortOptionTargets;
     LongOptionTargets  = longOptionTargets;
     ErrorHandlers      = errorHandlers;
 }
示例#2
0
 public Option(string text, Utils.EventHandler function = null, Option[] subOptions = null,
               OptGetter update = null)
 {
     Text         = text;
     DrawableText = new PlainText(text);
     SubOpts      = subOptions;
     Click        = function;
     HasFunction  = function != null;
     Activatable  = HasFunction || SubOpts != null;
     Updater      = update;
 }
示例#3
0
        private static OptGetter OptGetter()
        {
            var rv = new OptGetter();

            rv.AddOption('a', OptionType.Switch, "alpha");
            rv.AddOption('b', OptionType.Parameterized, "bravo");
            rv.AddOption('c', OptionType.MultiParameterized, "charlie");
            rv.AddOption("long-long", OptionType.Switch);
            rv.AddOption('x', OptionType.Parameterized);
            rv.AddAlias('x', "extreme", "extremely");

            rv.AcceptOptionAsValue = false;
            rv.EnableDashDash      = true;

            return(rv);
        }
示例#4
0
        public static void TestOptGetter(OptGetter opt, params string[] p)
        {
            // GetOptResult r = default;
            Exconsole.WriteLine(">" + p.JoinBy(" "));
            opt.BeginParse(p);

            bool eoa = false;

            while (!eoa)
            {
                GetOptError err;
                if ((err = opt.GetOpt(out var result)) == GetOptError.EndOfArguments)
                {
                    eoa = true;
                }

                Exconsole.WriteLine($"  {err}: {result.Type}: \"{Mix(result.Option, result.LongOption)}\", p: {result.Argument ?? "<null>"}, pp: {result.Arguments?.JoinBy(", ") ?? "<null>"}");
            }

            opt.EndParse();
        }
示例#5
0
        // ReSharper restore StaticMemberInGenericType

        private static CommandLineParserTypeMetaInfo GenerateOptGetter()
        {
            var opt = new OptGetter();
            var shortOptionTargets = new Dictionary <char, PropertyInfo>();
            var longOptionTargets  = new Dictionary <string, PropertyInfo>();

            var type = typeof(T);

            var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in props)
            {
                var        propType = prop.PropertyType;
                OptionType optionType;

                if (propType == typeof(bool))
                {
                    optionType = OptionType.Switch;
                }
                else if (propType == typeof(string))
                {
                    optionType = OptionType.Parameterized;
                }
                else if (propType == typeof(string[]))
                {
                    optionType = OptionType.MultiParameterized;
                }
                else
                {
                    continue;
                }

                foreach (var attr in prop.GetCustomAttributes <OptionAttribute>(true))
                {
                    if (attr.Option != '\0')
                    {
                        shortOptionTargets[attr.Option] = prop;
                        opt.AddOption(attr.Option, optionType);
                    }

                    if (attr.LongOption != null)
                    {
                        longOptionTargets[attr.LongOption] = prop;
                        opt.AddOption(attr.LongOption, optionType);
                    }
                }
            }

            var handlers = new List <MethodInfo>();
            var methods  = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var method in methods)
            {
                if (method.GetCustomAttribute <GetOptErrorHandlerAttribute>() != null)
                {
                    if (GetOptErrorHandlerAttribute.IsValidErrorHandler(method))
                    {
                        handlers.Add(method);
                    }
                }
            }

            return(new CommandLineParserTypeMetaInfo(opt, shortOptionTargets, longOptionTargets, handlers));
        }