commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.46k stars 473 forks source link

Backslash parsing problem in argument. #918

Open BoshenGuan opened 4 months ago

BoshenGuan commented 4 months ago

Describe the bug Read string option with backslash \ and " results in reading " in the string.

To Reproduce

public class Options
{
    [Option('P', "path", Required = true, HelpText = "Set working directory path.")]
    public string Path { get; set; }
}

static void Main(string[] args)
{
    string path = "";

    var result = Parser.Default.ParseArguments<Options>(args)
           .WithParsed(o =>
           {
               path = o.Path;
           });

    if (result.Tag == ParserResultType.Parsed)
    {
        Console.WriteLine($"Path: {path}");
    }
}
  1. Passing in argument -P "C:\path\to\read\"
  2. Console output is Path: C:\path\to\read"

Expected behavior Read backslash as it is. In the above example, output should be Path: C:\path\to\read\

Additional context Am i missing some options? Ref issues/226 reports the same problem, still not fixed?

elgonzo commented 4 months ago

This particular behavior is not a bug in the library.

Ref https://github.com/commandlineparser/commandline/issues/226 reports the same problem, still not fixed?

It's not caused by the library. Nor can it be solved by the library, as the library operates on the args array passed to the Main method. If you would have bothered and just spent 20 seconds to read the response in the issue you linked, you would know what is going on... :-1::unamused:

Inspect the args array you are passing, and you might be surprised by what you see.

(Basically, what you observe is the typical behavior by the program start-up code which is creating the args array for the Main method from the commandline string provided to the program. This behavior is equivalent to the WinAPI function GetCommandlineArgs. Plus, the syntax rules of the shell used to type in the commandline arguments also have an effect on what the commandline string passed to the program will look like, and thus also have an effect on what the content in the args array will ultimately look like.)