示例#1
0
        private void SealOptTable()
        {
            if (IsSealed)
            {
                return;
            }

            optTable = builder.CreateTable();
            IsSealed = true;
        }
示例#2
0
 public ArgumentListTest()
 {
     optTable = new OptTableBuilder()
                .AddFlag(OptA, "-", "a")
                .AddFlag(OptB, "-", "b")
                .AddJoined(OptC, "-", "c=")
                .AddJoined(OptD, "-", "d=")
                .AddFlag(OptE, "-", "e")
                .AddFlag(OptF, "-", "f")
                .CreateTable();
 }
示例#3
0
        public void DefinedOptions()
        {
            var options = new[] {
                new TestOption(1, "opt2"),
                new TestOption(2, "opt1")
            };

            var optTable = new OptTable(options);

            Assert.Equal(new[] { options[0], options[1] }, optTable.DefinedOptions);
        }
示例#4
0
        public void GetOption()
        {
            var options = new[] {
                new TestOption(1, "opt1"),
                new TestOption(2, "opt2")
            };

            var optTable = new OptTable(options);

            Assert.Equal(options[0], optTable.GetOption(1));
            Assert.Equal(options[1], optTable.GetOption(2));
            Assert.Null(optTable.GetOption(3));
        }
示例#5
0
        public void Alias(string input, string spelling)
        {
            var option   = new FlagOption(1, new[] { "-", "/", "--" }, "foo");
            var option2  = new FlagOption(2, new[] { "-", "/", "--" }, "bar", aliasId: 1);
            var optTable = new OptTable(new[] { option, option2 });

            int idx = 0;
            var arg = option2.Accept(new[] { input }, ref idx);

            Assert.NotNull(arg);
            Assert.Equal(1, arg.Option.Id);
            Assert.Equal(spelling, arg.Spelling);
            Assert.Null(arg.Value);
        }
示例#6
0
        public void Alias(string[] input, string spelling)
        {
            var option   = new MultiArgOption(1, new[] { "-", "/", "--" }, "qux", 2);
            var option2  = new MultiArgOption(2, new[] { "-", "/", "--" }, "foo", 2, aliasId: 1);
            var optTable = new OptTable(new[] { option, option2 });

            int idx = 0;
            var arg = option2.Accept(input, ref idx);

            Assert.NotNull(arg);
            Assert.Equal(1, arg.Option.Id);
            Assert.Equal(spelling, arg.Spelling);
            Assert.Equal("1", arg.Value);
            Assert.Equal(new[] { "1", "2" }, arg.Values);
        }
示例#7
0
        private static void ShowHelp(OptTable opts, IReadOnlyDictionary <string, OptTable> codeGenOpts)
        {
            Console.WriteLine("Usage: {0} [options] <input.man>", GetExeName());
            Console.WriteLine();
            var settings = new WriteHelpSettings();

            settings.MaxLineLength = Console.WindowWidth;
            opts.WriteHelp(Console.Out, settings);

            foreach (var pair in codeGenOpts.OrderBy(x => x.Key))
            {
                Console.WriteLine();
                pair.Value.WriteHelp(Console.Out, settings);
            }
        }
示例#8
0
        public static Arg MakeArg(
            this OptTable optTable, int id, string spelling, int index, string value = null)
        {
            Option option = optTable.GetOption(id);

            if (option is JoinedOption) // FIXME
            {
                value = value ?? string.Empty;
            }
            if (value != null)
            {
                return(new Arg(option, spelling, index, value));
            }
            return(new Arg(option, spelling, index));
        }
示例#9
0
        public IArgumentList Parse(IReadOnlyList <string> args)
        {
            SealOptTable();

            IArgumentList al = OptTable.ParseArgs(args, out _);

            foreach (var info in infos)
            {
                var attribute = info.Member.MemberInfo.GetCustomAttribute <OptionAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                attribute.PopulateValue(info.Member, info.OptionId, al);
            }

            return(al);
        }
示例#10
0
        public static Arg MakeArg(
            this OptTable optTable, int id, string spelling, int index, params string[] values)
        {
            values = values ?? new string[1];

            Option option = optTable.GetOption(id);

            if (option is JoinedOption) // FIXME
            {
                values[0] = values[0] ?? string.Empty;
            }
            if (values.Length > 1)
            {
                return(new Arg(option, spelling, index, values));
            }
            if (values[0] != null)
            {
                return(new Arg(option, spelling, index, values[0]));
            }
            return(new Arg(option, spelling, index));
        }
示例#11
0
        public void ConstructOptional()
        {
            var option = new FlagOption(
                1, "-", "foo", helpText: "help", aliasId: 2, groupId: 3,
                metaVar: "meta");

            Assert.Equal(1, option.Id);
            Assert.Equal("-", option.Prefix);
            Assert.Equal("foo", option.Name);
            Assert.Equal("help", option.HelpText);
            Assert.Equal("meta", option.MetaVar);
            Assert.Null(option.Alias);
            Assert.Null(option.Group);
            Assert.Same(option, option.UnaliasedOption);

            var option2 = new FlagOption(2, "-", "bar");
            var option3 = new FlagOption(3, "-", "qux");

            var optTable = new OptTable(new[] { option, option2, option3 });

            Assert.Same(option2, option.Alias);
            Assert.Same(option3, option.Group);
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref="NOptionTrackedToolTask"/> class.
 /// </summary>
 protected NOptionTrackedToolTask(OptTable optTable)
 {
     this.optTable = optTable;
 }
示例#13
0
        private static bool ParseOptions(
            string[] cliArgs, OptTable optTable, EmcCommandLineArguments arguments, DiagnosticsEngine diags)
        {
            var catalog = new AggregateCatalog();

            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(ICodeGeneratorProvider).Assembly));

            IArgumentList args = optTable.ParseArgs(cliArgs, out var missing);

            var extensions = args.GetAllArgValues(Opt.ext_eq);

            if (extensions.Count != 0)
            {
                foreach (var path in extensions)
                {
                    if (!File.Exists(path))
                    {
                        diags.Report(DiagnosticSeverity.Warning, "Ignoring extension assembly '{0}' because it does not exist.", path);
                        continue;
                    }
                    catalog.Catalogs.Add(new AssemblyCatalog(path));
                }
            }

            arguments.GeneratorOpts = new Dictionary <string, OptTable>();
            var codeGenerator = args.GetLastArgValue(Opt.generator_eq, arguments.CompilationOptions.CodeGenOptions.CodeGenerator);

            var container = new CompositionContainer(catalog);

            container.ComposeParts(diags);

            var codeGeneratorProviders = container.GetExportedValues <ICodeGeneratorProvider>().ToList();

            var    claimedArgs       = new HashSet <int>();
            object customCodeGenOpts = null;

            foreach (var provider in codeGeneratorProviders)
            {
                var info = new CodeGeneratorInfo(provider.Name, provider);
                if (info.OptTable != null)
                {
                    arguments.GeneratorOpts[provider.Name] = info.OptTable;
                }

                if (codeGenerator == provider.Name)
                {
                    customCodeGenOpts = info.ParseOptions(cliArgs, claimedArgs, diags);
                }
            }

            bool success = true;

            if (missing.ArgCount != 0)
            {
                diags.ReportError(
                    "Missing {1} argument{2} after: {0}",
                    cliArgs[missing.ArgIndex],
                    missing.ArgCount,
                    missing.ArgCount == 1 ? string.Empty : "s");
                success = false;
            }

            // Report errors on unknown arguments.
            foreach (var arg in args.Matching(Opt.Unknown))
            {
                if (claimedArgs.Contains(arg.Index) || IsExtensionArgument(arg))
                {
                    continue;
                }

                diags.ReportError("Unknown argument: {0}", arg.GetAsString());
                success = false;
            }

            arguments.ShowHelp          = args.HasArg(Opt.help) || args.HasArg(Opt.QuestionMark);
            arguments.ShowVersion       = args.HasArg(Opt.version);
            arguments.DumpMessageTable  = args.GetLastArgValue(Opt.dump_msg);
            arguments.DumpEventTemplate = args.GetLastArgValue(Opt.dump_wevt);
            arguments.OutputManifest    = args.GetLastArgValue(Opt.gen_manifest);
            arguments.Verify            = args.GetFlag(Opt.verify);

            var opts = arguments.CompilationOptions;

            opts.Inputs = args.GetAllArgValues(Opt.Input).ToList();
            opts.ResourceGenOnlyInputs = args.GetAllArgValues(Opt.resgen_manifest_eq).ToList();
            opts.OutputBaseName        = args.GetLastArgValue(Opt.out_eq);
            opts.GenerateResources     = args.GetFlag(Opt.res, Opt.no_res, true);
            opts.MessageTableFile      = args.GetLastArgValue(Opt.msg_file_eq);
            opts.EventTemplateFile     = args.GetLastArgValue(Opt.wevt_file_eq);
            opts.ResourceFile          = args.GetLastArgValue(Opt.rc_file_eq);

            opts.CodeGenOptions.GenerateCode   = args.GetFlag(Opt.code, Opt.no_code, true);
            opts.CodeGenOptions.CodeHeaderFile = args.GetLastArgValue(Opt.header_file_eq);
            opts.CodeGenOptions.CodeGenerator  = codeGenerator;

            if (opts.Inputs.Count == 1)
            {
                arguments.DecompilationOptions.InputModule = opts.Inputs[0];
            }
            else if (opts.Inputs.Count == 2)
            {
                arguments.DecompilationOptions.InputEventTemplate = opts.Inputs[0];
                arguments.DecompilationOptions.InputMessageTable  = opts.Inputs[1];
            }
            arguments.DecompilationOptions.OutputManifest = arguments.OutputManifest;

            var compatLevelString = args.GetLastArgValue(Opt.compat_eq, "10.0.16299");

            if (Version.TryParse(compatLevelString, out Version compatibilityLevel))
            {
                opts.CompatibilityLevel = compatibilityLevel;
            }
            else
            {
                diags.ReportError("Invalid compatibility level: {0}", compatLevelString);
                success = false;
            }

            opts.InferUnspecifiedOutputFiles();

            if (arguments.Verify)
            {
                opts.CodeGenOptions.GenerateCode = false;
                opts.GenerateResources           = false;
            }

            if (opts.CodeGenOptions.CodeGenerator != null)
            {
                var selectedCodeGen = codeGeneratorProviders.FirstOrDefault(x => x.Name == opts.CodeGenOptions.CodeGenerator);
                if (selectedCodeGen == null)
                {
                    diags.ReportError("No code generator found with name '{0}'.", opts.CodeGenOptions.CodeGenerator);
                    diags.Report(
                        DiagnosticSeverity.Note,
                        "Available generators: {0}",
                        string.Join(", ", codeGeneratorProviders.Select(x => x.Name)));
                    success = false;
                }
                else
                {
                    opts.CodeGenOptions.CodeGeneratorFactory = () => selectedCodeGen.CreateGenerator(customCodeGenOpts);
                }
            }

            return(success);
        }
示例#14
0
 public ArgTest()
 {
     optTable = new OptTable(GetOptions());
 }