示例#1
0
        public async Task Should_Pass_Case_1()
        {
            // 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.AddBranch <MammalSettings>("mammal", mammal =>
                    {
                        mammal.AddCommand <DogCommand>("dog");
                        mammal.AddCommand <HorseCommand>("horse");
                    });
                });
            });

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

            // Then
            result.ShouldBe(0);
            settings.Age.ShouldBe(12);
            settings.GoodBoy.ShouldBe(true);
            settings.IsAlive.ShouldBe(true);
            settings.Name.ShouldBe("Rufus");
        }
示例#2
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");
            });
        }
示例#3
0
        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_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);
            }
示例#5
0
            public static string Run <TSettings>(params string[] args)
                where TSettings : CommandSettings
            {
                using (var writer = new FakeConsole())
                {
                    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());
                }
            }
示例#6
0
            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_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));
        }
示例#8
0
        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");
        }
示例#9
0
            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");
            }
示例#10
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));
        }
示例#11
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_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 });
        }
示例#13
0
        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.AddCommand <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");
        }
示例#14
0
        public void Should_Register_Command_Settings_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

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

            // 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));
        }
示例#15
0
            public string Run(params string[] args)
            {
                using (var console = new FakeConsole())
                {
                    var app = new CommandApp();
                    _appConfiguration?.Invoke(app);

                    app.Configure(_configuration);
                    app.Configure(c => c.ConfigureConsole(console));
                    app.Run(args);

                    return(console.Output
                           .NormalizeLineEndings()
                           .TrimLines()
                           .Trim());
                }
            }
示例#16
0
        static async Task <int> Main(string[] args)
        {
            var app = new CommandApp(RegisterServices());

            app.Configure(config => ConfigureCommands(config));

            return(await app.RunAsync(args).ConfigureAwait(false));
        }
示例#17
0
        static Task Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(c =>
            {
            });

            return(app.RunAsync(args));
        }
示例#18
0
        static Task <int> Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <DoctorCommand>("doctor");
            });

            return(app.RunAsync(new[] { "doctor" }.Concat(args)));
        }
示例#19
0
    public static async Task <int> Main(string[] args)
    {
        var app = new CommandApp <DefaultCommand>();

        app.Configure(config =>
        {
            config.SetApplicationName("dotnet example");
        });

        return(await app.RunAsync(args).ConfigureAwait(false));
    }
示例#20
0
        public static int Main(string[] args)
        {
            var app = new CommandApp();

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

            return(app.Run(args));
        }
示例#21
0
        public (int ExitCode, string Output, CommandContext Context, CommandSettings Settings) Run(params string[] args)
        {
            CommandContext  context  = null;
            CommandSettings settings = null;
            var             writer   = new FakeConsoleWriter();

            var app = new CommandApp();

            _appConfiguration?.Invoke(app);

            app.Configure(_configuration);
            app.Configure(c => c.SetInterceptor(new ActionInterceptor((ctx, s) =>
            {
                context  = ctx;
                settings = s;
            })));
            app.Configure(c => c.SetOut(writer));
            var result = app.Run(args);

            return(result, writer.ToString(), context, settings);
        }
示例#22
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));
        }
示例#23
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));
        }
示例#24
0
    public static void ConfigureCommands(
        this CommandApp <RootCommand> app)
    {
        ArgumentNullException.ThrowIfNull(app);

        app.Configure(config =>
        {
            config.AddBranch(CommandConstants.NameOptionsFile, ConfigureOptionsFileCommands());
            config.AddBranch(NameCommandConstants.Generate, ConfigureGenerateCommands());
            config.AddBranch(NameCommandConstants.Validate, ConfigureValidateCommands());
        });
    }
示例#25
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));
        }
示例#26
0
        static void Main(string[] args)
        {
            var app = new CommandApp();

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

            app.Run(args);
        }
示例#27
0
        static Task <int> Main(string[] args)
        {
            var app = new CommandApp();

            app.Configure(config =>
            {
                config.AddCommand <ListCommand>("ls");
                config.AddCommand <DumpCommand>("dump");
            });

            return(app.RunAsync(args));
        }
示例#28
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));
        }
示例#29
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));
        }
示例#30
0
        public static async Task <int> Main(string[] args)
        {
            var registrar = new AutofacTypeRegistrar(BuildContainer());
            var app       = new CommandApp <BuildCommand>(registrar);

            app.Configure(config =>
            {
                config.SetApplicationName("advanced");
                config.AddCommand <BuildCommand>("build");
            });

            return(await app.RunAsync(args));
        }