Beispiel #1
0
        public void RegisterAndRunCommandWithoutOptions()
        {
            _application.AddCommand <TestCommand>("cmd");

            _application.Run(new[] { "cmd" });

            var command = Assert.IsType <TestCommand>(_commandFactory.CreatedCommand);

            Assert.True(command.HasBeenExecuted);
        }
Beispiel #2
0
        public int RunCommand(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            CommandLineApplication command = new CommandLineApplication(throwOnUnexpectedArg: true)
            {
                Name     = $"dotnet {CommandName}",
                FullName = FullCommandNameLocalized,
            };

            command.HelpOption("-h|--help");

            command.Argument(ArgumentName, ArgumentDescriptionLocalized);

            foreach (var subCommandCreator in SubCommands)
            {
                var subCommand = subCommandCreator();
                command.AddCommand(subCommand);

                subCommand.OnExecute(() => {
                    try
                    {
                        if (!command.Arguments.Any())
                        {
                            throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, ArgumentDescriptionLocalized);
                        }

                        var projectOrDirectory = command.Arguments.First().Value;
                        if (string.IsNullOrEmpty(projectOrDirectory))
                        {
                            projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
                        }

                        return(subCommand.Run(projectOrDirectory));
                    }
                    catch (GracefulException e)
                    {
                        Reporter.Error.WriteLine(e.Message.Red());
                        subCommand.ShowHelp();
                        return(1);
                    }
                });
            }

            try
            {
                return(command.Execute(args));
            }
            catch (GracefulException e)
            {
                Reporter.Error.WriteLine(e.Message.Red());
                command.ShowHelp();
                return(1);
            }
            catch (CommandParsingException e)
            {
                Reporter.Error.WriteLine(e.Message.Red());
                return(1);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var app = new CommandLineApplication();

            app.Name     = "custom-vision";
            app.FullName = "Azure Cognitive Services Custom Vision API Demostration";
            app.VersionOption("-v|--version", "v1.0");
            app.HelpOption("-?|-h|--help");

            Guid   projectId     = new Guid(Util.Configuration["projectId"]);
            string trainingKey   = Util.Configuration["trainingKey"];
            string predictionKey = Util.Configuration["predictionKey"];

            app.AddCommand(new UploadCommand(projectId, trainingKey))
            .AddCommand(new TrainCommand(projectId, trainingKey))
            .AddCommand(new PredictCommand(projectId, trainingKey, predictionKey));

            try
            {
                if (args.Length == 0)
                {
                    app.ShowHelp();
                }
                else
                {
                    app.Execute(args);
                }
            }
            catch (CommandParsingException cpe)
            {
                Console.WriteLine(cpe.Message);
                app.ShowHelp();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Example program with a command.
        /// </summary>
        /// <remarks>
        /// Try calling with e.g. <c>test -n 10 -o 3 -o 5 -o 7 --greet</c>
        /// </remarks>
        static void Main(string[] args)
        {
            var application = new CommandLineApplication();

            application.AddCommand <TestCommand>();

            application.Execute(args);
        }