https://github.com/Cysharp/ConsoleAppFramework
| |
| var app = ConsoleApp.Create() |
| .ConfigureServices(it => it.AddSingleton<DownloadCommander>()) |
| .ConfigureLogging(it => |
| { |
| 配置 zlogger |
| it.ClearProviders(); |
| it.SetMinimumLevel(LogLevel.Trace); |
| it.AddZLoggerConsole(options => options.SetLogFormat()); |
| // it.AddZLoggerFile("log.txt"); |
| it.AddZLoggerRollingFile(options => |
| { |
| options.FilePathSelector = (dt, index) => $"logs/{dt:yyyy-MM-dd}_{index}.log"; |
| options.RollingInterval = RollingInterval.Day; |
| options.RollingSizeKB = 1024 * 1024; |
| options.SetLogFormat(); |
| }); |
| }); |
| ; |
| |
| |
| app.Add("", async ([FromServices] DownloadCommander commander, CancellationToken cancellationToken) => { await commander.RunAsync(cancellationToken); }); |
| |
| |
| app.Add("Add", (int x, int y) => Console.WriteLine(x + y)); |
| |
| app.Add("Multiply", (double x, double y) => { Console.WriteLine(x * y); }); |
| |
| app.Add("Delay", async () => { await Task.Delay(TimeSpan.FromSeconds(5)); }); |
| |
| app.Run(args); |
| |
| public static class ZlogMessageFormattingTool |
| { |
| private static readonly ConcurrentDictionary<string, string> SrotNameDict = new(); |
| |
| public static void SetLogFormat(this ZLoggerOptions options, bool shortName = false) |
| { |
| options.UsePlainTextFormatter(formatter => |
| { |
| formatter.SetPrefixFormatter($"{0} [{1:short}] [{2}] ", |
| (in MessageTemplate template, in LogInfo info) => template.Format(info.Timestamp, info.LogLevel, GetCategoryName(info.Category, shortName))); |
| }); |
| } |
| |
| private static object GetCategoryName(LogCategory category, bool? shortName) |
| { |
| return shortName.GetValueOrDefault() ? category.Name : SrotNameDict.GetOrAdd(category.Name, name => name.Split('.').Last()); |
| } |
| } |
如果不用注入这一套,直接运行的话,如下
| ConsoleApp.Run(args, (int foo, int bar) => Console.WriteLine($"Sum: {foo + bar}")); |
| await ConsoleApp.RunAsync(args, async Task<int> (int foo, int bar, CancellationToken token) => { return 1;}); |
如果要将注释显示到命令的 --help 中。需要写一个类,在类函数中进行注释
[Command("")] 中设置为默认命令
简写参数名在注释中使用 -x,
注明,多个使用 | 分隔
[Argument] 顺序参数,不需要参数名
参数是数组的话,传入如下格式 [xx, xx, xx, ]
string optional = "abcde" 为指定参数,可忽略参数
bool 作为标志位解析,传入参数名为 true
enum 不区分大小写解析
一般参数使用 IParsable.TryParse
解析
如果是对象,自动使用 JsonSerializer.Deserialize<T>
解析
使用 params string[] paramsArray
接收不定长参数
| |
| app.Add<CommandHelper>(); |
| |
| app.Run(args); |
| |
| |
| public class CommandHelper |
| { |
| |
| |
| |
| |
| |
| |
| public async Task<int> RunAddAsync([Argument] int a, [Argument] int b, [FromServices] ILogger<CommandHelper> logger) |
| { |
| var result = a + b; |
| |
| |
| logger.ZLogInformation($"result: {result}"); |
| |
| return 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| [Command("")] |
| public async Task<int> RunAsync(int count, [FromServices] DownloadCommander commander, CancellationToken cancellationToken) |
| { |
| return await commander.RunAsync(count, cancellationToken); |
| } |
| } |
| https: |
| |
| https: |
| |
| https: |
| |
| https: |
| > Cocona |
| > Cocona.lite |
| |
| https: |
| > 项目说明:https: |
| > 对 rider 相当不友好 |
| |
| https: |
| https: |