using System.CommandLine; using System.CommandLine.NamingConventionBinder; using System.CommandLine.Invocation; using System.CommandLine.IO; using System.Reflection; namespace Project; public static class Program { public static async Task<Int32> Main(string[] args) { RootCommand rootCommand = new() { #if DEBUG new Command("debug", "debugging").WithHandler(nameof(HandleDebugAsync)), #endif // DEBUG new Command("run", "running") { new Option<string>(new[] { "--config", "-c" }, description: "config file path.", getDefaultValue: () => "config.json") { IsRequired = true, }, new Option<bool>(new[] { "--verbose", "-v" }, description: "Show the details.", getDefaultValue: () => false) { IsRequired = false, }, }.WithHandler(nameof(HandleRunAsync)), }; return await rootCommand.InvokeAsync(args); } #if DEBUG private static async Task<Int32> HandleDebugAsync(IConsole console, CancellationToken cancellationToken) { string configFilePath = "config.json"; bool verbose = true; return await HandleRunAsync(configFilePath, verbose, console, cancellationToken); } #endif // DEBUG private static async Task<Int32> HandleRunAsync(string configFilePath, bool verbose, IConsole console, CancellationToken cancellationToken) { if (verbose) { console.WriteLine($"current directory '{Environment.CurrentDirectory}'"); console.WriteLine($"--config '{configFilePath}'"); console.WriteLine($"--verbose '{verbose}'"); } await Task.CompletedTask; return 0; } private static Command WithHandler(this Command command, string methodName) { MethodInfo? method = typeof(Program).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static); ICommandHandler handler = CommandHandler.Create(method!); command.Handler = handler; return command; } }
'분류 전체보기'에 관한 글 390개
- 2022/03/14 System.CommandLine 사용
- 2020/04/21 CommandLine 시간 측정
- 2019/03/31 zookeeper session 시간 설정
- 2017/05/21 블로그 이전합니다(Github) 2
- 2016/07/26 GitExtension 한글 IME 깨지는 문제 해결 1
- 2016/07/11 [Jenkins] SVN Revision 변경 확인하기(cmd)
- 2016/06/22 [Docker] TimeZone을 Asia/Seoul로 변경하기
- 2016/06/22 [Docker] 한국어(ko_KR) locale 설정하기
- 2016/05/07 XProject를 OpenSource로 진행합니다.
- 2016/04/17 파일 이름을 숫자+알파벳만 남기기
System.CommandLine 사용
프로그래밍
2022/03/14 22:18
덧글을 달아 주세요