Win32 API command line arguments parsing
There is no Win32 support for parsing command-line arguments.
See related articles at MSDN:
Parsing C++ Command-Line Arguments
Argument Definitions
Customizing C++ Command-Line Processing
also look at similar questions:
What parameter parser libraries are there for C++?
Parsing parameters to main()
Option Parsers for C/C++?
What's an effective way to parse command line parameters in C++?
...
The only support that Win32 provides for command line arguments are the functions GetCommandLine
and CommandLineToArgvW
. This is exactly the same as the argv
parameter that you have for a console application.
You will have to do the parsing yourself. Regex would be a good option for this.
You could mess around with various libraries and stuff... But sometimes all you require is something simple, practical and quick:
int i;char *key, *value;for( i = 1; i <= argc; i++ ) { if( *argv[i] == '/' ) { key = argv[i] + 1; value = strchr(key, ':'); if( value != NULL ) *value++ = 0; process_option( key, value ); } else { process_value( argv[i] ); }}
You get the idea...
This is assuming a normal Win32 console app as you have implied (which has a traditional main
function). For Win32 apps you come in at WinMain
instead, as another person has already commented.