Exemplo n.º 1
0
        private CommandAppResult Run(string[] args, TestConsole console, Action <IConfigurator>?config = null)
        {
            CommandContext? context  = null;
            CommandSettings?settings = null;

            var app = new CommandApp();

            _appConfiguration?.Invoke(app);

            if (_configuration != null)
            {
                app.Configure(_configuration);
            }

            if (config != null)
            {
                app.Configure(config);
            }

            app.Configure(c => c.ConfigureConsole(console));
            app.Configure(c => c.SetInterceptor(new CallbackCommandInterceptor((ctx, s) =>
            {
                context  = ctx;
                settings = s;
            })));

            var result = app.Run(args);

            var output = console.Output
                         .NormalizeLineEndings()
                         .TrimLines()
                         .Trim();

            return(new CommandAppResult(result, output, context, settings));
        }
        public void Should_Pass_Case_3()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new DogSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            var result = app.Run(new[] { "animal", "dog", "12", "--good-boy", "--name", "Rufus" });

            // Then
            result.ShouldBe(0);
            settings.Age.ShouldBe(12);
            settings.GoodBoy.ShouldBe(true);
            settings.IsAlive.ShouldBe(false);
            settings.Name.ShouldBe("Rufus");
        }
Exemplo n.º 3
0
        public static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.SetApplicationName("fakedotnet");

                // Root command
                config.AddCommand <EfSettings>("ef", ef =>
                {
                    ef.SetDescription("Fake EF Core .NET Command Line Tools");

                    // Database
                    ef.AddCommand <EfCommandSettings>("database", database =>
                    {
                        database.AddCommand <EfUpdateCommand>("update");
                        database.AddCommand <EfDropCommand>("drop");
                    });

                    // DbContext
                    ef.AddCommand <EfCommandSettings>("dbcontext", dbcontext =>
                    {
                        dbcontext.AddCommand <EfScaffoldCommand>("scaffold");
                    });
                });
            });

            return(app.Run(args));
        }
            public void Should_Register_Remaining_Parsed_Arguments_With_Context()
            {
                // Given
                var capturedContext = default(CommandContext);

                var resolver = new FakeTypeResolver();
                var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

                resolver.Register(new DogSettings());
                resolver.Register(command);

                var app = new CommandApp(new FakeTypeRegistrar(resolver));

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddBranch <AnimalSettings>("animal", animal =>
                    {
                        animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                    });
                });

                // When
                app.Run(new[] { "animal", "4", "dog", "12", "--", "--foo", "bar", "--foo", "baz", "-bar", "\"baz\"", "qux" });

                // Then
                capturedContext.Remaining.Parsed.Count.ShouldBe(4);
                capturedContext.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
                capturedContext.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
                capturedContext.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
                capturedContext.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
            }
            public void Should_Register_Remaining_Raw_Arguments_With_Context()
            {
                // Given
                var capturedContext = default(CommandContext);

                var resolver = new FakeTypeResolver();
                var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

                resolver.Register(new DogSettings());
                resolver.Register(command);

                var app = new CommandApp(new FakeTypeRegistrar(resolver));

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddBranch <AnimalSettings>("animal", animal =>
                    {
                        animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                    });
                });

                // When
                app.Run(new[] { "animal", "4", "dog", "12", "--", "--foo", "bar", "-bar", "\"baz\"", "qux" });

                // Then
                capturedContext.Remaining.Raw.Count.ShouldBe(5);
                capturedContext.Remaining.Raw[0].ShouldBe("--foo");
                capturedContext.Remaining.Raw[1].ShouldBe("bar");
                capturedContext.Remaining.Raw[2].ShouldBe("-bar");
                capturedContext.Remaining.Raw[3].ShouldBe("\"baz\"");
                capturedContext.Remaining.Raw[4].ShouldBe("qux");
            }
        public void Should_Pass_Case_2()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new DogSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <DogCommand>("dog");
            });

            // When
            var result = app.Run(new[] { "dog", "12", "4", "--good-boy", "--name", "Rufus", "--alive" });

            // Then
            result.ShouldBe(0);
            settings.Legs.ShouldBe(12);
            settings.Age.ShouldBe(4);
            settings.GoodBoy.ShouldBe(true);
            settings.IsAlive.ShouldBe(true);
            settings.Name.ShouldBe("Rufus");
        }
        public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
        {
            // Given
            var capturedContext = default(CommandContext);

            var resolver = new FakeTypeResolver();
            var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

            resolver.Register(new DogSettings());
            resolver.Register(command);

            var registrar = new FakeTypeRegistrar(resolver);
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                });
            });

            // When
            var result = app.Run(new[] { "animal", "4", "dog", "12", "--foo" });

            // Then
            capturedContext.ShouldNotBeNull();
            capturedContext.Remaining.Parsed.Count.ShouldBe(1);
            capturedContext.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
        }
        public void Should_Pass_Case_5()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new OptionVectorSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <OptionVectorCommand>("multi");
            });

            // When
            var result = app.Run(new[] { "multi", "--foo", "a", "--foo", "b", "--bar", "1", "--foo", "c", "--bar", "2" });

            // Then
            result.ShouldBe(0);
            settings.Foo.Length.ShouldBe(3);
            settings.Foo.ShouldBe(new[] { "a", "b", "c" });
            settings.Bar.Length.ShouldBe(2);
            settings.Bar.ShouldBe(new[] { 1, 2 });
        }
Exemplo n.º 9
0
        static int Main(string[] args)
        {
            // to retrieve the log file name, we must first parse the command settings
            // this will require us to delay setting the file path for the file writer.
            // With serilog we can use an enricher and Serilog.Sinks.Map to dynamically
            // pull this setting.
            var serviceCollection = new ServiceCollection()
                                    .AddLogging(configure =>
                                                configure.AddSerilog(new LoggerConfiguration()
                                                                     // log level will be dynamically be controlled by our log interceptor upon running
                                                                     .MinimumLevel.ControlledBy(LogInterceptor.LogLevel)
                                                                     // the log enricher will add a new property with the log file path from the settings
                                                                     // that we can use to set the path dynamically
                                                                     .Enrich.With <LoggingEnricher>()
                                                                     // serilog.sinks.map will defer the configuration of the sink to be ondemand
                                                                     // allowing us to look at the properties set by the enricher to set the path appropriately
                                                                     .WriteTo.Map(LoggingEnricher.LogFilePathPropertyName,
                                                                                  (logFilePath, wt) => wt.File($"{logFilePath}"), 1)
                                                                     .CreateLogger()
                                                                     )
                                                );

            var registrar = new TypeRegistrar(serviceCollection);
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.SetInterceptor(new LogInterceptor()); // add the interceptor
                config.AddCommand <HelloCommand>("hello");
            });

            return(app.Run(args));
        }
Exemplo n.º 10
0
        public void Should_Register_Commands_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <GenericCommand <FooCommandSettings> >("foo");
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            app.Run(new[]
            {
                "animal", "4", "dog", "12",
            });

            // Then
            registrar.Registrations.ContainsKey(typeof(ICommand)).ShouldBeTrue();
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(GenericCommand <FooCommandSettings>));
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(DogCommand));
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(HorseCommand));
        }
Exemplo n.º 11
0
        public void Should_Register_Remaining_Arguments()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.AddCommand <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            app.Run(new[] { "animal", "--foo", "f", "dog", "--bar", "b", "--name", "Rufus" });

            // Then
            registrar.Instances.ContainsKey(typeof(IArguments)).ShouldBeTrue();
            registrar.Instances[typeof(IArguments)].Single().As <IArguments>(args =>
            {
                args.Count.ShouldBe(2);
                args.Contains("--foo").ShouldBeTrue();
                args["--foo"].Single().ShouldBe("f");
                args.Contains("--bar").ShouldBeTrue();
                args["--bar"].Single().ShouldBe("b");
            });
        }
Exemplo n.º 12
0
        public void Can_Register_Command_Settings_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();

            registrar.Register(typeof(DogSettings), typeof(DogSettings));
            registrar.Register(typeof(MammalSettings), typeof(MammalSettings));
            var app = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            app.Run(new[]
            {
                "animal", "4", "dog", "12",
            });

            // Then
            registrar.Registrations.ContainsKey(typeof(DogSettings)).ShouldBeTrue();
            registrar.Registrations[typeof(DogSettings)].Count.ShouldBe(1);
            registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
            registrar.Registrations.ContainsKey(typeof(MammalSettings)).ShouldBeTrue();
            registrar.Registrations[typeof(MammalSettings)].Count.ShouldBe(1);
            registrar.Registrations[typeof(MammalSettings)].ShouldContain(typeof(MammalSettings));
        }
Exemplo n.º 13
0
            public void Should_Execute_Nested_Delegate_Command()
            {
                // Given
                var dog  = default(DogSettings);
                var data = 0;

                var app = new CommandApp();

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddBranch <AnimalSettings>("foo", foo =>
                    {
                        foo.AddDelegate <DogSettings>(
                            "bar", (context, settings) =>
                        {
                            dog  = settings;
                            data = (int)context.Data;
                            return(1);
                        }).WithData(2);
                    });
                });

                // When
                var result = app.Run(new[] { "foo", "4", "bar", "12" });

                // Then
                result.ShouldBe(1);
                dog.ShouldNotBeNull();
                dog.Age.ShouldBe(12);
                dog.Legs.ShouldBe(4);
                data.ShouldBe(2);
            }
Exemplo n.º 14
0
        public static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <TableGeneratorCommand>("tables");
            });

            return(app.Run(args));
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <ServeCommand>("serve");
                config.AddCommand <ExportCommand>("export");
            });

            app.Run(args);
        }
Exemplo n.º 16
0
        public static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <ColorGeneratorCommand>("colors");
                config.AddCommand <EmojiGeneratorCommand>("emoji");
            });

            return(app.Run(args));
        }
Exemplo n.º 17
0
        public static int Main(string[] args)
        {
            var app = new CommandApp <GenerateCommand>();

            app.Configure(config =>
            {
                config.AddCommand <GenerateCommand>("generate");
                config.AddCommand <InspectCommand>("inspect");
            });

            return(app.Run(args));
        }
Exemplo n.º 18
0
        static int Main(string[] args)
        {
            var commandApp = new CommandApp();

            commandApp.Configure(x =>
            {
                x.AddCommand <NotificationRepeatCommand>("run");
                x.AddCommand <RegisterCommand>("register");
                x.AddCommand <UnRegisterCommand>("deregister");
            });
            return(commandApp.Run(args));
        }
Exemplo n.º 19
0
        static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <UploadCommand>("upload");

                config.AddCommand <DownloadCommand>("download");
            });

            return(app.Run(args));
        }
Exemplo n.º 20
0
        static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <ImportCommand>("import");
                config.AddCommand <AnalyzeCommand>("analyze");
                config.AddCommand <SelectCommand>("select");
            });

            return(app.Run(args));
        }
Exemplo n.º 21
0
    public static int Main(string[] args)
    {
        var container = BuildContainer();

        var app = new CommandApp <DefaultCommand>(container);

        app.Configure(config =>
        {
            config.SetApplicationName("theproject");
            config.UseStrictParsing();
        });

        return(app.Run(args));
    }
Exemplo n.º 22
0
            public string Run(params string[] args)
            {
                var writer = new FakeConsoleWriter();

                var app = new CommandApp();

                _appConfiguration?.Invoke(app);

                app.Configure(_configuration);
                app.Configure(c => c.SetOut(writer));
                app.Run(args);

                return(writer.ToString());
            }
Exemplo n.º 23
0
        static int Main(string[] args)
        {
            var services = BuildServices().AddConfiguration();
            var app      = new CommandApp(new DependencyInjectionRegistrar(services));

            app.Configure(config => {
                config.SetApplicationName("Memery CLI");
                config.AddCommand <Commands.UploadCommand>("upload");
                config.AddCommand <Commands.AddRemoteImageCommand>("add-image");
                config.AddCommand <Commands.ListCommand>("list");
                config.AddCommand <Commands.InfoCommand>("info");
            });
            return(app.Run(args.RemoveHelp()));
        }
Exemplo n.º 24
0
    public static int Main(string[] args)
    {
        var app = new CommandApp();

        app.Configure(config =>
        {
            config.AddDelegate("foo", Foo)
            .WithDescription("Foos the bars");

            config.AddDelegate <BarSettings>("bar", Bar)
            .WithDescription("Bars the foos");;
        });

        return(app.Run(args));
    }
Exemplo n.º 25
0
        static void Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddProxy <ProxyCommandSettings>("proxy", c =>
                {
                    c.AddCommand <EditCommand>("edit");
                    c.AddCommand <DeleteCommand>("delete");
                    c.AddCommand <CreateCommand>("create");
                });
            });
            app.Run(args);
        }
Exemplo n.º 26
0
        public static int Main(string[] args)
        {
            // Create a type registrar and register any dependencies.
            // A type registrar is an adapter for a DI framework.
            var registrations = new ServiceCollection();

            registrations.AddSingleton <IGreeter, HelloWorldGreeter>();
            var registrar = new TypeRegistrar(registrations);

            // Create a new command app with the registrar
            // and run it with the provided arguments.
            var app = new CommandApp <DefaultCommand>(registrar);

            return(app.Run(args));
        }
Exemplo n.º 27
0
            public void Should_Not_Propagate_Exceptions_If_Not_Explicitly_Told_To_Do_So()
            {
                // Given
                var app = new CommandApp();

                app.Configure(config =>
                {
                    config.AddCommand <ThrowingCommand>("throw");
                });

                // When
                var result = app.Run(new[] { "throw" });

                // Then
                result.ShouldBe(-1);
            }
Exemplo n.º 28
0
        internal static void Main(string[] args)
        {
            var configurationPointer = new EmbeddedConfigurationPointer
            {
                FileExtension = ".json",
                FileName      = "app-configuration"
            };

            var configurationLoader  = new EmbeddedConfigurationLoader("Configuration");
            var commandConfiguration = configurationLoader.GetConfiguration <CommandConfiguration>(configurationPointer);

            var app = new CommandApp();

            app.Configure(commandConfiguration.ConfigureCommand);
            app.Run(args);
        }
Exemplo n.º 29
0
        public static int Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                foreach (var index in Enumerable.Range(1, 10))
                {
                    config.AddCommand <MyCommand>($"c{index}")
                    .WithDescription($"Prints the number {index}")
                    .WithData(index);
                }
            });

            return(app.Run(args));
        }
Exemplo n.º 30
0
        public static string Run <TSettings>(params string[] args)
            where TSettings : CommandSettings
        {
            using (var writer = new TestConsole())
            {
                var app = new CommandApp();
                app.Configure(c => c.ConfigureConsole(writer));
                app.Configure(c => c.AddCommand <GenericCommand <TSettings> >("foo"));
                app.Run(args);

                return(writer.Output
                       .NormalizeLineEndings()
                       .TrimLines()
                       .Trim());
            }
        }