コード例 #1
0
        /// <summary>
        /// Add a <see cref="ICommandTypeProvider"/> based on browsing all types in all loaded assemblies after having loaded all assemblies present on the current folder.
        /// </summary>
        /// <param name="builder">The <see cref="CommandLineParserBuilder"/> to configure.</param>
        /// <returns>The <see cref="CommandLineParserBuilder" /> so that additional calls can be chained.</returns>
        public static CommandLineParserBuilder AddClassBasedCommands(this CommandLineParserBuilder builder)
        {
            builder.Services.TryAddSingleton <IAssemblyProvider, CurrentDirectoryAssemblyProvider>();
            builder.Services.TryAddScoped <ICommandTypeProvider, AssemblyBrowsingClassBasedCommandTypeProvider>();
            builder.Services.TryAddScoped <IClassBasedCommandActivator, ClassBasedDependencyResolverCommandActivator>();

            return(builder);
        }
コード例 #2
0
        /// <summary>
        /// Add a <see cref="ICommandTypeProvider"/> based on browsing all types in the assembly containing the specified <typeparamref name="TCommand"/>.
        /// </summary>
        /// <typeparam name="TCommand">The command type used to specify the assembly to browse.</typeparam>
        /// <param name="builder">The <see cref="CommandLineParserBuilder"/> to configure.</param>
        /// <returns>The <see cref="CommandLineParserBuilder" /> so that additional calls can be chained.</returns>
        public static CommandLineParserBuilder AddCommands <TCommand>(this CommandLineParserBuilder builder)
            where TCommand : class, ICommand
        {
            builder.Services.TryAddSingleton <IAssemblyProvider>(new DefaultAssemblyProvider(typeof(TCommand).Assembly));
            builder.Services.TryAddScoped <ICommandTypeProvider, AssemblyBrowsingClassBasedCommandTypeProvider>();
            builder.Services.TryAddScoped <IClassBasedCommandActivator, ClassBasedDependencyResolverCommandActivator>();

            return(builder);
        }
        /// <summary>
        /// Add a lambda-based command.
        /// </summary>
        /// <param name="builder">The <see cref="CommandLineParserBuilder"/> to configure.</param>
        /// <param name="commandName">The name of the command.</param>
        /// <param name="defineCommand">An action to define the command.</param>
        /// <param name="executeCommand">A function that represent the execution of the command.</param>
        /// <returns>The <see cref="CommandLineParserBuilder" /> so that additional calls can be chained.</returns>
        public static CommandLineParserBuilder AddCommand(this CommandLineParserBuilder builder,
                                                          string commandName,
                                                          Action <CommandBuilder> defineCommand,
                                                          Func <CommandExecutionContext, Task <int> > executeCommand)
        {
            var commandBuilder = new CommandBuilder(commandName, executeCommand);

            (defineCommand ?? (_ => {}))(commandBuilder);

            builder.Services.AddScoped <ICommandTypeProvider>(serviceProvider => new LambdaBasedCommandTypeProvider(commandBuilder.BuildCommandType(serviceProvider)));

            return(builder);
        }