Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.54k stars 88 forks source link

improve parsing of command-line arguments #85

Closed wipiano closed 1 year ago

wipiano commented 1 year ago

Sample Application

I have a console application that takes a DateTime argument as follows:

var app = ConsoleApp.Create(args);
app.AddRootCommand((DateTime dt) =>
{
    Console.WriteLine($"dt: {dt}");
});
app.Run();

Current Behavior

Example 1: Enclose parameter value in escaped double quotations.

> ConsoleApp.exe --dt '\"2022-07-07\"'
# dt: 2022/07/07 0:00:00

This will work correctly, but following Example 2 will not work.

Example 2: Parameter value only (without escaped double quotations).

> ConsoleApp.exe --dt "2022-07-07"
# Parameter "dt" fail on JSON deserialize, please check type or JSON escape or add double-quotation. args: --dt 2022-07-07

New Behavior

Example 1: Enclose parameter value in escaped double quotations.

> ConsoleApp.exe --dt '\"2022-07-07\"'
# dt: 2022/07/07 0:00:00

This works correctly as before.

Example 2: Parameter value only (without escaped double quotations).

We don't need to enclose parameter values in escaped double quotes now.

> ConsoleApp.exe --dt "2022-07-07"
dt: 2022/07/07 0:00:00
wipiano commented 1 year ago

@neuecc

neuecc commented 1 year ago

thanks! I'll release soon.