示例#1
0
        public OptionSet Add(string prototype, string description, Action <string> action, bool hidden)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            string[] names  = prototype.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            int?     mainId = null;

            foreach (var name in names)
            {
                int id = nextOptionId++;

                if (name.EndsWith("="))
                {
                    builder.AddJoined(id, "-", name, helpText: description, aliasId: mainId);
                }
                else
                {
                    builder.AddFlag(id, "-", name, helpText: description, aliasId: mainId);
                }

                mainId = mainId ?? id;
                actions.Add(id, Tuple.Create(action));
            }

            return(this);
        }
示例#2
0
            private OptTable ReflectOptTable(Type optionType)
            {
                if (optionType is null)
                {
                    return(null);
                }

                int optIdx  = 0;
                var groupId = Opt.custom + optIdx++;

                var builder = new OptTableBuilder()
                              .AddUnknown(Opt.Unknown)
                              .AddGroup(groupId, name, helpText: $"CodeGen Options (Generator {name})");

                foreach (var property in optionType.GetTypeInfo().DeclaredProperties)
                {
                    var optionAttribute = property.GetCustomAttributes <OptionAttribute>().FirstOrDefault();
                    if (optionAttribute is null)
                    {
                        continue;
                    }

                    var helpText = optionAttribute.HelpText ?? optionAttribute.Name;
                    var optionId = Opt.custom + optIdx++;

                    switch (optionAttribute)
                    {
                    case JoinedOptionAttribute x:
                        builder.AddJoined(optionId, "-", $"c{optionAttribute.Name}:", helpText: helpText, groupId: groupId);
                        infos.Add(new JoinedOptionInfo(x, optionId, property));
                        break;

                    case FlagOptionAttribute x:
                        builder.AddFlag(optionId, "-", $"c{optionAttribute.Name}", helpText: helpText, groupId: groupId);
                        infos.Add(new FlagOptionInfo(x, optionId, property));
                        break;
                    }
                }

                return(builder.CreateTable());
            }