wxWidgets中的命令行参数

jac*_*ack 4 c++ command-line wxwidgets

有没有办法可以读取传递给C++ wxWidgets应用程序的命令行参数?如果是这样,请您提供一个如何操作的示例.

Joh*_*nPS 5

在普通的C++中,有argcargv.当你正在建设一个wxWidgets的应用程序,你可以访问这些使用wxApp::argc,wxApp::argv[]或者wxAppConsole::argc,wxAppConsole::argv[].请注意,这wxApp是源自wxAppConsole,因此取决于您是否有控制台应用程序或GUI应用程序.请参阅wxAppConsole

IMPLEMENT_APP(MyApp)

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

您可能也对wxCmdLineParser感兴趣.


Ano*_*ous 1

看一下这些示例 ( 1 , 2 ) 或:

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; 
}
Run Code Online (Sandbox Code Playgroud)