Exemplo n.º 1
0
        public Task CommandAppAsyncTest(string[] args,
                                        string commandName,
                                        Action <CommandConfiguration <Models.BuildOptions> > configure,
                                        Models.BuildOptions expectedOptions)
        {
            var configuration = new ApplicationConfiguration <Models.Options>();

            configuration
            .Command <Models.BuildOptions>(commandName, cfg =>
            {
                configure(cfg);
                cfg.OnExecuteAsync(opt =>
                {
                    opt.ShouldBe(expectedOptions);
                    return(Task.CompletedTask);
                });
            })
            .PositionArgument(arg => arg.Map.ToProperty(o => o.Project))
            .OnExecuteAsync(_ =>
            {
                false.ShouldBeTrue("App handler called, shouldn't been");
                return(Task.CompletedTask);
            });
            return(RuntimeCommandBuilder.Build(configuration, args, out var options)
                   .InvokeAsync(options, CancellationToken.None));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var config = new ApplicationConfiguration <Options>();

            // Configure run command
            config
            .Command <RunOptions>("run", cmd => cmd
                                  .Switch("--sprint", arg => arg.Map.ToProperty(opt => opt.Sprint))
                                  // Notice we are "overriding" the pace option, and changing the validation range because
                                  // we're running. The 60-120 rule will take precedence over the 30-60 rule if the user
                                  // decides to run
                                  .Option <int>("-p|--pace", arg => arg
                                                .Map.ToProperty(opt => opt.Pace)
                                                .Validate.Between(60, 120, messageFormat: (_, value) => $"Pace should be between 60 and 120 steps per minute (you put {value}...)"))
                                  .OnExecute(Run));

            // Configure walk command
            config
            .Command <WalkOptions>("walk", cmd => cmd
                                   .Switch("--skip", arg => arg.Map.ToProperty(opt => opt.Skip))
                                   .OnExecute(Walk));

            // Configure main application stuff
            config
            .HelpOption("-h|--help")
            .Help.UseContent(HelpContent)
            // Main/base/root application options and switches common to all commands
            .Option("-d|--destination", arg => arg.Map.ToProperty(opt => opt.Destination))
            .Option <int>("-p|--pace", arg => arg
                          .Map.ToProperty(opt => opt.Pace)
                          .Validate.Between(30, 60, messageFormat: (_, value) => $"Pace should be between 30 and 60 steps per minute (you put {value}...)"))
            // Here we are insisting that the user specify a command
            .OnExecute(_ => throw new Exception("You must run or walk..."));

            try
            {
                CommandLineApplication.Run(config, args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Type -h or --help to get help.");
            }
        }
Exemplo n.º 3
0
        public void HelpInvokedWhenCommandConfigured()
        {
            var config     = new ApplicationConfiguration <Models.Options>();
            var writerMock = new Mock <IHelpWriter>();

            writerMock.Setup(m => m.WriteContent(It.IsAny <IReadOnlyCollection <string> >())).Verifiable();
            config.Command <Models.BuildOptions>("build", _ => { })
            .HelpOption("--help", writerMock.Object)
            .Help.UseContent(new[] { "help" });
            RuntimeCommandBuilder.Build(config, new[] { "--help" }, out var options).Invoke(options);
            writerMock.Verify(m => m.WriteContent(It.IsAny <IReadOnlyCollection <string> >()), Times.Once);
        }
Exemplo n.º 4
0
        public void CommandAppTest(string[] args,
                                   string commandName,
                                   Action <CommandConfiguration <Models.BuildOptions> > configure,
                                   Models.BuildOptions expectedOptions)
        {
            var configuration = new ApplicationConfiguration <Models.Options>();

            configuration
            .Command <Models.BuildOptions>(commandName, cfg =>
            {
                configure(cfg);
                cfg.OnExecute(opt => opt.ShouldBe(expectedOptions));
            })
            .PositionArgument(arg => arg.Map.ToProperty(o => o.Project))
            .OnExecute(_ => false.ShouldBeTrue("App handler called, shouldn't been"));
            RuntimeCommandBuilder.Build(configuration, args, out var options).Invoke(options);
        }
Exemplo n.º 5
0
 public void CommandCreatesSubConfiguration()
 {
     _instanceUnderTest.Command("test", _ => { });
     _instanceUnderTest.SubConfigurations.Single().ShouldNotBeNull();
 }