command line switches on gui wx program 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
kambasana
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Aug 26, 2005 10:47 pm
Location: Walsall UK

command line switches on gui wx program

Post by kambasana » Thu Sep 01, 2005 12:31 am

hi,

i would like to know if i make a gui wx program can i add command line switches to it so if im on windows or linux i can type the program name and then some commands that it understands.

Such as Myapp.exe /a

For example the the /a would be automatic mode.

how can i do this?

thanks

raboof
Earned a small fee
Earned a small fee
Posts: 20
Joined: Fri Sep 10, 2004 10:44 pm

Re: command line switches on gui wx program

Post by raboof » Thu Sep 01, 2005 12:42 am

kambasana wrote:I would like to know if i make a gui wx program can i add command line switches to it so if im on windows or linux i can type the program name and then some commands that it understands.
You can use wxApp::argv and wxApp::argc

kambasana
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Aug 26, 2005 10:47 pm
Location: Walsall UK

Post by kambasana » Thu Sep 01, 2005 12:55 am

so can i make a pure gui-less wx app all commandline driven then link a wx gui to it? it that possible?

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Sep 01, 2005 6:41 am

kambasana wrote:so can i make a pure gui-less wx app all commandline driven then link a wx gui to it? it that possible?
This would be one way to go.
But you can create a normal GUI app first, too. Commandline options are the same for GUI and CUI apps. Every GUI app can be called with command line options (for opening files etc.). Just use wxApp::argv and wxApp::argc as raboof pointed out. The contents will be the same as with normal argv and argc. Another class that may be interesting is wxCmdLineParser.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Thu Sep 01, 2005 12:44 pm

You can use wxApp::OnInitCmdLine and wxApp::OnCmdLineParsed like in any command line app.

I have a GUI app using command line parameters. It can even completely run without GUI (so called "silent" mode).

kambasana
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Aug 26, 2005 10:47 pm
Location: Walsall UK

Post by kambasana » Thu Sep 01, 2005 10:39 pm

benedicte wrote:You can use wxApp::OnInitCmdLine and wxApp::OnCmdLineParsed like in any command line app.

I have a GUI app using command line parameters. It can even completely run without GUI (so called "silent" mode).
Thats sounds great thanks guys, benedicte would you be able to show me an example of your GUI app using command line parameters, it sounds interesting.

thanks

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Fri Sep 02, 2005 8:11 am

in main .h file

Code: Select all

class MyApp: public wxApp
{
public:
// constructor/desctructor...

    // from wxApp
    virtual bool OnInit();
    virtual int OnExit();
    virtual int OnRun();
    virtual void OnInitCmdLine(wxCmdLineParser& parser);
    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);

private:

// your functions and members
};
in main .cpp file

Code: Select all

const wxCmdLineEntryDesc gCmdLineDesc[] =
{
	{ wxCMD_LINE_SWITCH, wxT("H"), wxT("help"), wxT("Display usage info"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
	{ wxCMD_LINE_SWITCH, wxT("s"), wxT("silent"), wxT("Silent mode errors"), wxCMD_LINE_VAL_NONE, 0 },
	{ wxCMD_LINE_OPTION, NULL, wxT("o"), wxT("output mode: plain text, HTML or XML"), wxCMD_LINE_VAL_STRING, 0 },
	{ wxCMD_LINE_PARAM, NULL, NULL, wxT("File(s)/Directory(ies) concerned by operation"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE},
	{ wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0},
};

bool myApp::OnInit()
{
   wxApp::OnInit();
// some application-dependent treatments...
  // Show the frame
  main_frame = new wxFrame...
  main_frame->Show(true);
  SetTopWindow(main_frame);
  return true;
}

int myApp::OnExit()
{
    // clean up
    return 0;
}

int FHGuiMergeApp::OnRun()
{
       int exitcode = wxApp::OnRun();
       wxTheClipboard->Flush();
       if (exitcode!=0)
          return exitcode;
 }

void myApp::OnInitCmdLine(wxCmdLineParser& parser)
{
   parser.SetDesc (gCmdLineDesc);
   // must refuse '/' as parameter starter or cannot use "/path" style paths
   parser.SetSwitchChars (wxT("-"));
}

//--------------------------------------------
bool myApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
    silent_mode = parser.Found(wxT("s"));
// and other command line parameters

// then do what you need with them.

    return true;
}
You may also need to look for wxApp::CleanUp() and wxApp::ProcessIdle().

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Fri Sep 02, 2005 8:47 am

I have copied my previous post in the Wiki (http://wiki.wxwidgets.org/).

If you wish to update things with your experience, fell free.

morya
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Dec 14, 2007 2:29 am
Location: Xuzhou, China

Post by morya » Tue Dec 18, 2007 1:14 am

This is my first post here,
I just post to get me registered for this post.
It's so important, does fix what I need.
Thanks to benedicte! :wink:

By the way, I think line 27 in file "main.cpp" should be
MyApp::OnRun()
.

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Fri Sep 19, 2008 12:27 am

WOW. all this Command Line parsing and setting acceptibles and what not is giving me a head ache. I just want to execute my program with one simple option like.

Program.exe -65930

and throw that number into a variable when the program runs.

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Fri Sep 19, 2008 2:24 am

This is not working as expected no matter what i seem to try. I MUST be missing something. The Found command returns true ok, but I can't get the value.

Code: Select all

//// App.h 

#include <wx/wxprec.h>
#include <wx/cmdline.h>

#ifdef __BORLANDC__ 
	#pragma hdrstop 
#endif 

#ifndef WX_PRECOMP 
    #include <wx/wx.h> 
#endif 

const wxCmdLineEntryDesc gCmdLineDesc[] =
{
    { wxCMD_LINE_OPTION, "id", "WID",    "Windows Process ID", wxCMD_LINE_VAL_NUMBER },
    { wxCMD_LINE_NONE }
}; 

/////////////////////////////////////////////////////////////////////////////////
class MyApp : public wxApp
{

public:
    MyApp();
	~MyApp();

    // create our main window here
    virtual bool OnInit();
	virtual void OnInitCmdLine(wxCmdLineParser& parser);
    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);

private:
    DECLARE_NO_COPY_CLASS(MyApp)

};

DECLARE_APP(MyApp)

Code: Select all

//// App.cpp
#include "App.h" 

IMPLEMENT_APP(MyApp); 

bool MyApp::OnInit()
{ 
	    if ( !wxApp::OnInit() ) return false;

		MyFrame *frame = new MyFrame(0L, wxT("Updater 1.0b")); 

		SetTopWindow(frame); 	
		frame->Show();
		frame->Refresh();

		return true;
}

void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
    parser.SetDesc (gCmdLineDesc);
    // must refuse '/' as parameter starter or cannot use "/path" style paths
    parser.SetSwitchChars (wxT("-"));
}
 
bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
	parser.Parse();
    long *value;
	if(parser.Found(wxT("WID"), value)){
		wxMessageBox(wxString::Format("%d\n",value));
	}
 
    return true;
}

DavidHart
Site Admin
Site Admin
Posts: 3839
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Fri Sep 19, 2008 10:36 am

Hi,

You're passing a long pointer to wxCmdLineParser::Found (a null one btw, which the doc says not to do). You then try to get the pointer itself, not its contents, into the wxString, which isn't what you want.

Try:

Code: Select all

        long value;
        if(parser.Found(wxT("WID"), &value)){
                wxMessageBox(wxString::Format("%d\n",value));
        }
Regards,

David

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Fri Sep 19, 2008 3:36 pm

That fixed it :D I can't believe I overlooked that.
Thanks Dave

Post Reply