简体   繁体   中英

Command line arguments in wxWidgets

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.

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 .

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; 
}

您可以从您访问命令行变量wxApp ,因为它从inherites wxAppConsole提供wxAppConsole :: ARGCwxAppConsole :: argv的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM