Command line arguments in wxWidgets

2019-05-09 13:05发布

Is there a way I can read the command line arguments passed into a C++ wxWidgets application? If so, could you please provide an example of how to do so.

3条回答
【Aperson】
2楼-- · 2019-05-09 13:32

Have a look at these examples (1, 2) or:

int main(int argc, char **argv) 
{ 
    wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program"); 

    wxInitializer initializer; 
    if (!initializer) 
    { 
        fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); 
        return -1; 
    } 

    static const wxCmdLineEntryDesc cmdLineDesc[] = 
    { 
        { wxCMD_LINE_SWITCH, "h", "help", "show this help message", 
            wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, 
        // ... your other command line options here... 

        { wxCMD_LINE_NONE } 
    }; 

    wxCmdLineParser parser(cmdLineDesc, argc, wxArgv); 
    switch ( parser.Parse() ) 
    { 
        case -1: 
            wxLogMessage(_T("Help was given, terminating.")); 
            break; 

        case 0: 
            // everything is ok; proceed 
            break; 

        default: 
            wxLogMessage(_T("Syntax error detected, aborting.")); 
            break; 
    } 
    return 0; 
}
查看更多
不美不萌又怎样
3楼-- · 2019-05-09 13:39

You can access the command line variables from your wxApp as it inherites from wxAppConsole which provides wxAppConsole::argc and wxAppConsole::argv.

查看更多
女痞
4楼-- · 2019-05-09 13:48

In plain C++, there is argc and argv. When you are building a wxWidgets application, you can access these using wxApp::argc, wxApp::argv[] or wxAppConsole::argc, wxAppConsole::argv[]. Note that wxApp is derived from wxAppConsole, so either works depending on if you have a console app or GUI app. See wxAppConsole

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {
// Access command line arguments with wxApp::argc, wxApp::argv[0], etc.
// ...
}

You may also be interested in wxCmdLineParser.

查看更多
登录 后发表回答