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;
    }
}
				'프로그래밍'에 관한 글 206개
- 2022/03/14 System.CommandLine 사용
- 2016/07/26 GitExtension 한글 IME 깨지는 문제 해결 1
- 2014/10/19 [BATCH] N개의 작업을 병렬로 실행하고 종료될 때까지 대기하기
- 2014/10/04 [VisualStudio] inline 함수 최적화 옵션(/Ob2)
- 2014/09/13 [Git] git 저장소 크기 줄이기
- 2014/09/03 [C++11] 스레드를 생성하여 비동기 작업 처리하기
- 2014/07/10 Beyond compare 4 Pro 설정
- 2014/04/19 [log4cplus] property file
- 2014/04/10 Windows에서 Pylint 설치하기
- 2014/04/09 Pylint visual studio에서 사용하기
System.CommandLine 사용
						프로그래밍
						2022/03/14 22:18
						
					
				
덧글을 달아 주세요