Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

117 Commits

Repository files navigation

Smart.CommandLine - System.CommandLine hosting

NuGet

What is this?

  • Command-line application (CLI) development framework inspired by ASP.NET Core hosting model
  • Microsoft.Extensions ecosystem integration
  • POCO-based command definition
  • Attribute-based option mapping
  • Hierarchical sub-command structure
  • Global and command-specific filters for cross-cutting concerns (logging, exception handling, performance monitoring)
  • Source generator for metadata optimization

Usage example

usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;usingSmart.CommandLine.Hosting;// Command hostvarbuilder=CommandHost.CreateBuilder(args).UseDefaults();// Loggingbuilder.Services.AddLogging(logging =>{logging.ClearProviders();logging.AddSimpleConsole(options =>{options.SingleLine=true;options.TimestampFormat="HH:mm:ss.fff ";options.IncludeScopes=true;});});// Servicesbuilder.Services.AddSingleton<GreetService>();// Commandsbuilder.ConfigureCommands(commands =>{commands.ConfigureRootCommand(root =>{root.WithDescription("Sample CLI tool");});commands.AddGlobalFilter<ExecutionTimeFilter>(order:-100);commands.AddGlobalFilter<ExceptionHandlingFilter>(order:Int32.MaxValue);commands.AddCommand<MessageCommand>();commands.AddCommand<GreetCommand>();commands.AddCommand<FilterCommand>();commands.AddCommand<ExceptionCommand>();commands.AddCommand<UserCommand>(user =>{user.AddSubCommand<UserListCommand>();user.AddSubCommand<UserAddCommand>();user.AddSubCommand<UserRoleCommand>(role =>{role.AddSubCommand<UserRoleAssignCommand>();role.AddSubCommand<UserRoleRemoveCommand>();});});});varhost=builder.Build();returnawaithost.RunAsync();

Feature

Command

  • Example
[Command("message","Display text")]publicsealedclassMessageCommand:ICommandHandler{privatereadonlyILogger<MessageCommand>log;publicMessageCommand(ILogger<MessageCommand>log){this.log=log;}[Option<string>("--text","-t",Description="Text to show",Required=true)]publicstringText{get;set;}=default!;publicValueTaskExecuteAsync(CommandContextcontext){log.LogInformation("Show {Text}",Text);returnValueTask.CompletedTask;}}

Filter

  • Example
publicsealedclassLoggingFilter:ICommandFilter{privatereadonlyILogger<LoggingFilter>log;publicLoggingFilter(ILogger<LoggingFilter>log){this.log=log;}publicasyncValueTaskExecuteAsync(CommandContextcontext,CommandDelegatenext){log.LogInformation("Start command: {CommandType}",context.CommandType.Name);try{awaitnext(context);}finally{log.LogInformation("End command: {CommandType}",context.CommandType.Name);}}}

About

System.CommandLine hosting framework

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages