1

In C# I parse a path ending with '\', like this: -p "C:\My Folder\". The problem is, when parsing I get C:\My Folder" (with the final quote), instead of just C:\My Folder. Is there a way to avoid this problem? I've already tried with CommandLineParser and NDesk.Options, both ended up with the same problem...

The Options class:

    class Options
{
    [Option('p', "path", Required = false)]
    public string parsedPath { get; set; }

    [Option('f', "file", Required = false)]
    public string parsedFile { get; set; }

    [Option('l', "label", Required = false)]
    public string parsedLabel { get; set; }

    [Option('t', "torrent", DefaultValue = false)]
    public bool isTorrent { get; set; }
}

The code implementation:

var options = new Options();
CommandLine.Parser parser = new CommandLine.Parser();
parser.ParseArguments(args, options);

EDIT: I'm using the Command Line Parser Library library

wonea
  • 4,783
  • 17
  • 86
  • 139
Gabriel Duarte
  • 974
  • 1
  • 13
  • 28
  • 2
    Why is this a problem? – OMGtechy Dec 22 '14 at 19:37
  • the path is a string so it will show up in the debugger as `quoted text " "` what is the real issue you are facing or experiencing here..? – MethodMan Dec 22 '14 at 19:40
  • 1
    Which "CommandLineParser" class is this? – John Saunders Dec 22 '14 at 19:41
  • Your example above shows an escaped double quote though? – Rowland Shaw Dec 22 '14 at 19:43
  • @JohnSaunders Looks like it's the [Command Line Parser Library](https://commandline.codeplex.com/). – mason Dec 22 '14 at 19:53
  • @mason: yeah, I found that in Google, but would like the OP to tell us so we don't have to guess. – John Saunders Dec 22 '14 at 20:01
  • Take a look @ http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp – John Dec 22 '14 at 20:22
  • @JohnSaunders It is indeed https://commandline.codeplex.com/, i've edited the question... – Gabriel Duarte Dec 22 '14 at 22:18
  • @OMGtechy Its a problem because later i have to give this path to move files, and it doesn't find the path with the quote in the ending... – Gabriel Duarte Dec 22 '14 at 22:19
  • @DJKRAZE in visual studio, it appears like "C:\My Files\"", but when i for example print the result, it shows C:\My Files". What i wanted to show is C:\My Files\ without the last quote – Gabriel Duarte Dec 22 '14 at 22:24
  • @GabrielDuarte you need to escape all backslashes in the string – OMGtechy Dec 22 '14 at 22:24
  • If in Visual Studio it shows as `"C:\My Files\""` then it **is** `C:\My Files"` because Visual Studio shows it escaped for C#, where `"` is shown as `\"`. The code is fine, it's either the input data that is wrong (it should really be `"C:\My Files\\"` or `"C:\My Files"`) or your expectations (if that's the correct input, you're complaining about getting the correct output). – Jon Hanna Dec 22 '14 at 22:28
  • @OMGtechy The problem is that i pass this string via an argument (not in the code), like that: -p "C:\My Path\" so the last quote is escaped by mistake, and interpretated by Command Line Parser like C:\My Path" instead of C:\My Path\ – Gabriel Duarte Dec 22 '14 at 22:29
  • @GabrielDuarte then the problem is in the code calling this, not this function. You're passing a ", so it's giving you a ". – OMGtechy Dec 22 '14 at 22:33

1 Answers1

0

You have to escape the back slash: -p "C:\My Folder\\"

TL;DR

Escaping M (\M) does not escape but escaping quote (\") does. Alas, what happens is that you tried to escape the quote to be a part of your string to produce (C:\My Folder"). I can totally understand why this happens but it is a bit non-intuitive.
I stumbled upon the same, but from the opposite direction. My parameter was -myPath C:\what ever\ --verbose but that resulted in MyPath being C:\what. So I added quotes -myPath "C:\what ever\" --verbose and MyPath became C:\what ever" --verbose.

LosManos
  • 7,195
  • 6
  • 56
  • 107