Exemple #1
0
        // Uses the System.CommandLine package to parse the program arguments.
        private static Arguments ParseArguments(string[] args)
        {
            // Create a root command with some options
            var rootCommand = new System.CommandLine.RootCommand
            {
                new System.CommandLine.Option <bool>(
                    new string[] { "--pub", "-p" },
                    description: "Whether to run the publisher application"),
                new System.CommandLine.Option <bool>(
                    new string[] { "--sub", "-s" },
                    description: "Whether to run the subscriber application"),
                new System.CommandLine.Option <uint>(
                    new string[] { "--period", "-t" },
                    getDefaultValue: () => 0,
                    description: "The flow-controller token-bucket period in milliseconds  (0 = use value defined in XML profile)"),
                new System.CommandLine.Option <int>(
                    new string[] { "--domain", "-d" },
                    getDefaultValue: () => 0,
                    description: "Domain ID used to create the DomainParticipant"),
                new System.CommandLine.Option <int>(
                    new string[] { "--sample-count", "-c" },
                    getDefaultValue: () => int.MaxValue,
                    description: "Number of samples to publish or subscribe to"),
                new System.CommandLine.Option <bool>(
                    "--version",
                    description: "Displays the RTI Connext version"),
            };

            rootCommand.Description = "Custom FlowController Example";

            Arguments result = null;

            rootCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create(
                (Arguments arguments) => result = arguments);

            System.CommandLine.CommandExtensions.Invoke(rootCommand, args);

            if (result == null)
            {
                return(null);
            }

            if (result.Version)
            {
                Console.WriteLine(Rti.Dds.Core.ServiceEnvironment.Instance.Version);
                return(null);
            }

            if (!result.Pub && !result.Sub)
            {
                Console.WriteLine(rootCommand.Description);
                Console.WriteLine("\nYou can specify --pub or --sub to choose which application to run (or -h for help).");
                Console.WriteLine("For example:\n    dotnet run -- --pub\n");
                Console.Write("Which one do you want to run? Enter 'pub' or 'sub' > ");
                var choice = Console.ReadLine();
                result.Pub = choice.StartsWith("p", StringComparison.OrdinalIgnoreCase);
            }

            if (result.SampleCount < 0)
            {
                result.SampleCount = int.MaxValue;
            }

            if (result.Sub && result.Period != 0)
            {
                Console.WriteLine("--period can only be set when --pub is set");
            }

            return(result);
        }
        // Uses the System.CommandLine package to parse the program arguments.
        private static Arguments ParseArguments(string[] args)
        {
            // Create a root command with some options
            var rootCommand = new System.CommandLine.RootCommand
            {
                new System.CommandLine.Option <bool>(
                    new string[] { "--pub", "-p" },
                    description: "Whether to run the publisher application"),
                new System.CommandLine.Option <bool>(
                    new string[] { "--sub", "-s" },
                    description: "Whether to run the subscriber application"),
                new System.CommandLine.Option <string>(
                    new string[] { "--type-source" },
                    getDefaultValue: () => "build",
                    description: "How to get the DynamicType: idl, xml, build, or extended"),
                new System.CommandLine.Option <int>(
                    new string[] { "--domain", "-d" },
                    getDefaultValue: () => 0,
                    description: "Domain ID used to create the DomainParticipant"),
                new System.CommandLine.Option <int>(
                    new string[] { "--sample-count", "-c" },
                    getDefaultValue: () => int.MaxValue,
                    description: "Number of samples to publish or subscribe to"),
                new System.CommandLine.Option <bool>(
                    "--verbose",
                    description: "Increases the RTI Connext logging verbosity"),
                new System.CommandLine.Option <bool>(
                    "--version",
                    description: "Displays the RTI Connext version")
            };

            rootCommand.Description = "Example RTI Connext Publisher and Subscriber."
                                      + "\nSee README.txt for more information";

            Arguments result = null;

            rootCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create(
                (Arguments arguments) => result = arguments);

            System.CommandLine.CommandExtensions.Invoke(rootCommand, args);

            if (result == null)
            {
                return(null);
            }

            if (result.Version)
            {
                Console.WriteLine(Rti.Dds.Core.ServiceEnvironment.Instance.Version);
                return(null);
            }

            if (!result.Pub && !result.Sub)
            {
                Console.WriteLine(rootCommand.Description);
                Console.WriteLine("\nYou can specify --pub or --sub to choose which application to run (or -h for help).");
                Console.WriteLine("For example:\n    dotnet run -- --pub\n");
                Console.Write("Which one do you want to run? Enter 'pub' or 'sub' > ");
                var choice = Console.ReadLine();
                result.Pub = choice.StartsWith("p", StringComparison.OrdinalIgnoreCase);
            }

            if (result.SampleCount < 0)
            {
                result.SampleCount = int.MaxValue;
            }

            return(result);
        }
        // Uses the System.CommandLine package to parse the program arguments.
        private static Arguments ParseArguments(string[] args)
        {
            // Create a root command with some options
            var rootCommand = new System.CommandLine.RootCommand
            {
                new System.CommandLine.Option <bool>(
                    new string[] { "--pub", "-p" },
                    description: "Whether to run the publisher application"),
                new System.CommandLine.Option <bool>(
                    new string[] { "--sub", "-s" },
                    description: "Whether to run the subscriber application"),
                new System.CommandLine.Option <string>(
                    "--password",
                    description: "Example password used to authenticate [required] (see the example README)"),
                new System.CommandLine.Option <string>(
                    "--topic",
                    getDefaultValue: () => "Example msg",
                    description: "The topic name to publish or subscribe to"),
                new System.CommandLine.Option <int>(
                    new string[] { "--domain", "-d" },
                    getDefaultValue: () => 0,
                    description: "Domain ID used to create the DomainParticipant"),
                new System.CommandLine.Option <int>(
                    new string[] { "--sample-count", "-c" },
                    getDefaultValue: () => int.MaxValue,
                    description: "Number of samples to publish or subscribe to"),
                new System.CommandLine.Option <bool>(
                    "--verbose",
                    description: "Increases the RTI Connext logging verbosity"),
                new System.CommandLine.Option <bool>(
                    "--version",
                    description: "Displays the RTI Connext version")
            };

            rootCommand.Description = "Builtin Topics Example";

            Arguments result = null;

            rootCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create(
                (Arguments arguments) => result = arguments);

            System.CommandLine.CommandExtensions.Invoke(rootCommand, args);

            if (result == null)
            {
                return(null);
            }

            if (result.Version)
            {
                Console.WriteLine(Rti.Dds.Core.ServiceEnvironment.Instance.Version);
                return(null);
            }

            if (result.Password == null)
            {
                Console.WriteLine("The option --password is required");
                Console.WriteLine("Enter for example: dotnet run -- --pub --password <password>");
                return(null);
            }

            if (!result.Pub && !result.Sub)
            {
                Console.WriteLine(rootCommand.Description);
                Console.WriteLine("\nYou can specify --pub or --sub to choose which application to run (or -h for help).");
                Console.WriteLine("For example:\n    dotnet run -- --pub --password <password>\n");
                Console.Write("Which one do you want to run? Enter 'pub' or 'sub' > ");
                var choice = Console.ReadLine();
                if (choice.StartsWith("p", StringComparison.OrdinalIgnoreCase))
                {
                    result.Pub = true;
                }
                else
                {
                    result.Sub = true;
                }
            }

            if (result.SampleCount < 0)
            {
                result.SampleCount = int.MaxValue;
            }

            return(result);
        }