Win32 API command line arguments parsing

前端 未结 9 1975
小蘑菇
小蘑菇 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:15

    I have been developing and using libparamset that is written in plain C. It is really powerful and works well on Windows. It provides:

    • Is cross-platform.
    • Wildcard support for file input on Windows!
    • Powerful features. See libparamset.
    0 讨论(0)
  • 2020-12-16 14:15

    Not sure about existence of such a win32 api function(s), but Boost.Program_Options library could help you.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 14:24

    Just for the record, if you use MinGW's GCC, rather than Microsoft's MSVC, you get GNU getopt, (which also includes getopt_long and getopt_long_only variants), included within the standard runtime library.

    0 讨论(0)
  • 2020-12-16 14:32

    I don't believe that there is a Win32 API available. You can look for a Windows implementation of getopt or another library.

    0 讨论(0)
  • 2020-12-16 14:33

    You can parse the arguments by using GetCommandLine, PathRemoveArgs, PathGetArgs in a loop

    https://msdn.microsoft.com/en-us/library/windows/desktop/bb773742(v=vs.85).aspx

    0 讨论(0)
提交回复
热议问题