0

How does the OS break up command line arguments into an array?

I have a custom interpreter for an application that takes a hideously large number of arguments in complex combinations. I absolutely do not want to use the third-party solutions out there. I have a map set up that allows me to parse and generate these parameters.

While writing unit tests, I would like to take a generated string and split it exactly the way the OS does. I could launch a process with the generated string but I don't want to do that and pollute the unit tests.

Surely Windows must have a function it uses to do this before firing up the executable. I assume that's why we can simply pop the argument array reference off the stack in our entry point.

Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • The command arguments are already split into an array: static void Main(string[] args). Or are you wanting to split a string yourself? string[] args = "arg1 arg2 arg3".Split(" ".ToCharArray()); – Cory Jul 27 '14 at 00:40
  • 1
    Windows does no splitting. The executable gets one giant string and is responsible for doing it own splitting according to its own rules. Most applications rely on the C runtime to do the splitting. – Raymond Chen Jul 27 '14 at 02:39
  • 1
    @RaymondChen: Windows doesn't do the splitting during process startup, but it does provide [a common implementation](http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391%28v=vs.85%29.aspx) which programs can use to get consistent behavior instead of rolling their own rules. – Ben Voigt Jul 27 '14 at 06:30
  • @BenVoigt: Exactly what I was looking for, thanks. You should post this as an answer. – Raheel Khan Jul 27 '14 at 06:46
  • @mikez: Good catch. I did not see that before. – Raheel Khan Jul 27 '14 at 06:49

0 Answers0