1

Question: Is there a function in .NET (or PInvokable) that would prepare a command line (in the shape of one string); from a IEnumerable of strings.

That would be the equivalent of python's subproces.list2cmdline, but in .NET.

And it would be the reverse of Win32's CommandLineToArgvW.

As a sketch, it could probably be grossly approximated by:

static public string List2CmdLine(IEnumerable<string> args)
{
    return string.Join(" ", args.Select((s) => "\"" + s + "\"")))
}

details:
python's subprocess.list2cmdline documentation states that what they do, goes by respecting the rules of the Windows C Runtime.
I imagine they speak of the need of quotes around arguments which contains spaces, and escaping the quotes inside arguments.

v.oddou
  • 6,476
  • 3
  • 32
  • 63
  • `Environment.CommandLine` maybe? – leppie Jan 21 '15 at 10:35
  • @leppie that would be good for the case of simply recovering the original command line I gues ? which I grant you, is the case of my example. But for future googlers I wish to find a generic answer. – v.oddou Jan 22 '15 at 01:59
  • You mention your goal, but what exactly is your question? How double quotes are to be escaped within each argument? – C.Evenhuis Jan 22 '15 at 07:44
  • the question is : "is there any such function in .NET ?" and how are double quotes escaped I don't know and that is exactly the point. I don't want to know, I want a function that does is all correctly, just like `list2cmdline` – v.oddou Jan 22 '15 at 09:14
  • @C.Evenhuis edited the question to be clearer. i hope.. – v.oddou Jan 22 '15 at 09:21
  • @v.oddou I'm afraid there is no built-in method in .NET, and I'm not aware of any native method that encodes multiple arguments into a single command-line. The rules (I know, you don't want to know these) aren't very complicated though: https://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx – C.Evenhuis Jan 22 '15 at 09:29
  • possible duplicate of [Escape command line arguments in c#](http://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp) – Erti-Chris Eelmaa Jan 22 '15 at 09:30

1 Answers1

0

There is no such built-in function. You can write your own (translated from Python function).

public static string ListToCommandLine(IEnumerable<string> args)
{
    return string.Join(" ", args.Select(a =>
    {
        if (string.IsNullOrEmpty(a)) 
            return "\"\"";

        var sb = new StringBuilder();

        var needQuote = a.Contains(' ') || a.Contains('\t');
        if (needQuote)
            sb.Append('"');

        var backslashCount = 0;

        foreach (var c in a)
        {
            switch (c)
            {
                case '\\':
                    backslashCount++;
                    break;

                case '"':
                    sb.Append(new string('\\', backslashCount * 2));
                    backslashCount = 0;
                    break;

                default:
                    if (backslashCount > 0)
                    {
                        sb.Append(new string('\\', backslashCount));
                        backslashCount = 0;
                    }

                    sb.Append(c);
                    break;            
            }

            if (backslashCount > 0)
                sb.Append(new string('\\', backslashCount * (needQuote ? 2 : 1)));

            if (needQuote)
                sb.Append('"');           
        }

        return sb.ToString();       
    }));
}
oryol
  • 5,178
  • 2
  • 23
  • 18
  • this is very interesting, i'm going to check that out in depth. for the moment I consider it as accepted :) also I'm sure I'm not the only one it will help in the future. – v.oddou Feb 06 '15 at 00:58