System.CommandLine seems to be incorrectly parsing arguments that are file paths that end with a trailing slash

43 views Asked by At

I am creating a console app using the System.CommandLine library. I have found what I think is a bug but would like to check if I'm just missing something.

using System.CommandLine;

class Program
{
    static async Task<int> Main(string[] args)
    {
        var sourceOption = new Option<DirectoryInfo>(
            "--source",
            "Source directory"
        );

        var destinationOption = new Option<DirectoryInfo>(
            "--destination",
            "Destination directory"
        );

        var rootCommand = new RootCommand("Test app for argument parsing")
        {
            sourceOption,
            destinationOption
        };

        rootCommand.SetHandler<DirectoryInfo, DirectoryInfo>((source, destination) => {
            Console.WriteLine($"Source: {source?.FullName ?? "Not provided"}");
            Console.WriteLine($"Destination: {destination?.FullName ?? "Not provided"}");
        },
        sourceOption, destinationOption);

        return await rootCommand.InvokeAsync(args);
    }
}

If you pass in a path argument that ends in a trailing slash and map it to a DirectoryInfo object, it seems to interpret the trailing backslash as an escape character that escapes the closing quotation mark of the argument value and then interprets the rest of the command line arguments string as the rest of the argument value.

For example: myApp.exe --source "e:\source" --destination "e:\destination" works as expected and outputs

Source: e:\source
Destination: e:\destination

However the following --source "e:\source\" --destination "e:\destination" the difference being the trailing slash on "e:\source\" parses incorrectly and outputs:

Source: e:\source" --destination e:\destination
Destination: Not provided

It appears to have interpreted this \" as an escape sequence for " and then --destination "e:\destination as the rest of the --source argument.

I struggle to imagine a world in which this is intended behaviour but I would appreciate any feedback from other people before I log a bug report.

0

There are 0 answers