Backslash and quote in command-line arguments

前端 未结 5 779
攒了一身酷
攒了一身酷 2020-11-29 06:53

Is the following behaviour some feature or a bug in C# .NET?

Test application:

using System;
using System.Linq;

namespace ConsoleApplication1
{
             


        
5条回答
  •  天涯浪人
    2020-11-29 07:26

    I have escaped the problem the other way...

    Instead of getting arguments already parsed I am getting the arguments string as it is and then I am using my own parser:

    static void Main(string[] args)
    {
        var param = ParseString(Environment.CommandLine);
        ...
    }
    
    // The following template implements the following notation:
    // -key1 = some value   -key2 = "some value even with '-' character "  ...
    private const string ParameterQuery = "\\-(?\\w+)\\s*=\\s*(\"(?[^\"]*)\"|(?[^\\-]*))\\s*";
    
    private static Dictionary ParseString(string value)
    {
       var regex = new Regex(ParameterQuery);
       return regex.Matches(value).Cast().ToDictionary(m => m.Groups["key"].Value, m => m.Groups["value"].Value);
    }
    

    This concept lets you type quotes without the escape prefix.

提交回复
热议问题