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.

5
  • 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()); Commented Jul 27, 2014 at 0: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. Commented Jul 27, 2014 at 2:39
  • 1
    @RaymondChen: Windows doesn't do the splitting during process startup, but it does provide a common implementation which programs can use to get consistent behavior instead of rolling their own rules. Commented Jul 27, 2014 at 6:30
  • @BenVoigt: Exactly what I was looking for, thanks. You should post this as an answer. Commented Jul 27, 2014 at 6:46
  • @mikez: Good catch. I did not see that before. Commented Jul 27, 2014 at 6:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.