Win32 API command line arguments parsing

前端 未结 9 2358
小蘑菇
小蘑菇 2020-12-16 13:52

I\'m writing Win32 console application, which can be started with optional arguments like this:

app.exe /argName1:\"argValue\" /argName2:\"argValue\"
         


        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 14:22

    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.

提交回复
热议问题