0

I need a hand in terms of processing command line argument (on Windows) in C.

Suppose I have the following situation

C:\Users\USER\Desktop> my_executable arg1 ' "A>200 && B<300 (just some conditions" ' 

In this case argc = 5

and

C:\Users\USER\Desktop> my_executable arg1 '"A>200 && B<300 (just some conditions"' 

In this case argc = 3

Depending on users, the argv and argc will be different. How can I write the code such that the condition and arg1 can be stored correctly :) Required: arg1 is stored into a char pointer condition is also stored into a char pointer

Thanks

Kenlucius
  • 41
  • 1
  • 8

1 Answers1

1

Don't use single quotes as argument quotes on Windows unless you want to implement your own argument parser. ^ can be used to escape " and itself and a few other things. To embed " in arguments use "".

If you really need to, call GetCommandLineW and parse yourself. GetCommandLineW returns a string that consists of the executable image name possibly enclosed in double quotes, followed by an optional space and the arguments exactly as given to CreateProcess (which means that ^ processing has already taken place).

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • So on window how should the " " type argument be passed? Should they simply be " something "??? What about on Mac??? Thank you :) – Kenlucius Nov 01 '16 at 04:15
  • what do you mean by `how should the " " type argument be passed?`? It's not easy to escape `"` in every cases in Windows – phuclv Nov 01 '16 at 04:25
  • @LưuVĩnhPhúc Oh I mean. How should we pass " condition " to command line? If we don't use single quotes like in my case. Is it simply my_executable arg1 "conditions" ??? – Kenlucius Nov 01 '16 at 04:32