How to add command line arguments for wxApp? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

How to add command line arguments for wxApp?

Post by apoorv569 »

I want to add some command line arguments to my application. My application is a graphical wxApp. I want to add a few options that basically returns something, for example, if I run my application from the terminal with some options passed along with it,

Code: Select all

./myApp  --version
should return something like,

Code: Select all

myApp v0.1.x
myApp's description...
And probably more arguments like this. How can I achieve this?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add command line arguments for wxApp?

Post by doublemax »

wxCmdLineParser
https://docs.wxwidgets.org/trunk/classw ... arser.html

A few of the sample use it, e.g. the webview sample to pass an URL to open.

Or you can directly use wxAppConsole::argc / wxAppConsole::argv like you might be used to:
https://docs.wxwidgets.org/trunk/classw ... 31a5e8bfc5
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: How to add command line arguments for wxApp?

Post by apoorv569 »

doublemax wrote: Sat Jul 24, 2021 9:30 am wxCmdLineParser
https://docs.wxwidgets.org/trunk/classw ... arser.html

A few of the sample use it, e.g. the webview sample to pass an URL to open.

Or you can directly use wxAppConsole::argc / wxAppConsole::argv like you might be used to:
https://docs.wxwidgets.org/trunk/classw ... 31a5e8bfc5
Is one of them better than the other, in terms of performance or something?

I tried wxCmdLineParser, seems pretty straight forward looking at the webview sample. I added these 2 functions in class inheriting from wxApp,

Code: Select all

void App::OnInitCmdLine(wxCmdLineParser& parser)
{
    wxApp::OnInitCmdLine(parser);

    parser.AddSwitch("v", "version", "Shows the application version", 0);
    parser.Parse(true);
}

bool App::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if (!wxApp::OnCmdLineParsed(parser))
        return false;

    if (parser.Found("version"))
        std::cout << "SampleHive v0.8.4_alpha.1" << std::endl;

    return true;
}
This does work, but the application starts as soon as it prints the version, can I tell it don't run the app when specific arguments are passed?

Also I can just added all other options I want in the OnInitCmdLine() like

Code: Select all

    parser.AddSwitch("a", "another option", "Shows some output", 0);
    parser.Parse(true);
    
    parser.AddSwitch("b", "another option", "Shows some output", 0);
    parser.Parse(true);

    parser.AddSwitch("c", "another option", "Shows some output", 0);
    parser.Parse(true);
and so on..?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add command line arguments for wxApp?

Post by doublemax »

This does work, but the application starts as soon as it prints the version, can I tell it don't run the app when specific arguments are passed?
Return "false" from App::OnCmdLineParsed or App::OnInit.
and so on..?
You only need to call Parse() once at the end.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: How to add command line arguments for wxApp?

Post by apoorv569 »

Return "false" from App::OnCmdLineParsed or App::OnInit.
Like this?

Code: Select all

bool App::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if (!wxApp::OnCmdLineParsed(parser))
        return false;

    if (parser.Found("version"))
        std::cout << "SampleHive v0.8.4_alpha.1" << std::endl;

    return false;        // <-------
}
You only need to call Parse() once at the end.
I see.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add command line arguments for wxApp?

Post by doublemax »

apoorv569 wrote: Sat Jul 24, 2021 3:55 pm
Return "false" from App::OnCmdLineParsed or App::OnInit.
Like this?

Code: Select all

bool App::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if (!wxApp::OnCmdLineParsed(parser))
        return false;

    if (parser.Found("version"))
        std::cout << "SampleHive v0.8.4_alpha.1" << std::endl;

    return false;        // <-------
}
Yes, but of course conditionally, depending on whether you want the app to exit or not.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: How to add command line arguments for wxApp?

Post by apoorv569 »

doublemax wrote: Sat Jul 24, 2021 5:57 pm
apoorv569 wrote: Sat Jul 24, 2021 3:55 pm
Return "false" from App::OnCmdLineParsed or App::OnInit.
Like this?

Code: Select all

bool App::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if (!wxApp::OnCmdLineParsed(parser))
        return false;

    if (parser.Found("version"))
        std::cout << "SampleHive v0.8.4_alpha.1" << std::endl;

    return false;        // <-------
}
Yes, but of course conditionally, depending on whether you want the app to exit or not.
Okay, thank you!
Post Reply