Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslash parsing problem in argument. #918

Open
BoshenGuan opened this issue Feb 28, 2024 · 1 comment
Open

Backslash parsing problem in argument. #918

BoshenGuan opened this issue Feb 28, 2024 · 1 comment

Comments

@BoshenGuan
Copy link

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
Copy link

elgonzo commented Feb 28, 2024

This particular behavior is not a bug in the library.

Ref #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... 👎😒

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants