Vladimir Prus


Wednesday, July 14, 2004

Windows command line

Users keept asking if the program_options library can parse the command line passed to WinMain. The problem is that this command line is just a string, and not array of string, so I decided I'd figure out how that string should be tokenized. The rules turned out to be rather simple, and even minimal, but confusing notwithstanding.

The queston, of course, is how to pass a argument with embedded spaces. The solution is standard -- quoting. So

show_args.exe "here we" go
will pass two arguments to the program, with embedded space in it.

Then, it's surely good to be able to insert just quote character, without any special meaning. Of course, we need to quote it with backslash:

show_args.exe "the movie \"untitled\""

We probably might want to add literal backslash before quotation mark. I'm already at lost why anybody would need it but anyway:

show_args.exe \\"here we go"
would pass "\here we go" as a single argument. Since we might want to have literal backslash followed by literal quote, we can add another backslash above, and so on till infinity, or rather until we run out of 1K of memory that command line can occupy.

This sounds reasonable, right? But there's one little detail: the backslashes need to have special meaning only before quote. Anywere else double backslash need not be treated specially. This is pretty interesting observration, and is exactly what happens in windows command line. Which proves that not all smart ideas lead to good interface -- the fact that \\ means one backslash before quote and two backslashes elsewhere is so confusing. I'd even say it's the second worst command line syntax idea -- the first being that all options should come before all arguments.

No comments: