Discussion:
Command line parsing
Kenneth Porter
2007-03-11 05:48:58 UTC
Permalink
Can anyone point me to some code that illustrates proper use of the command
line parsing machinery? I looked through the samples directory and only see
one very tepid use of it, which doesn't call the base wxApp::OnInit method.
How do I invoke that and still use the parser system?

For now my initial app needs a parameter to indicate which of several
sections in a configuration file should be used, eg. invocation would be
"myapp config1" or "myapp config2", with a default config assumed if no
parameter is passed.
Vadim Zeitlin
2007-03-11 12:00:29 UTC
Permalink
On Sat, 10 Mar 2007 23:48:58 -0600 Kenneth Porter <***@sewingwitch.com> wrote:

KP> For now my initial app needs a parameter to indicate which of several
KP> sections in a configuration file should be used, eg. invocation would be
KP> "myapp config1" or "myapp config2", with a default config assumed if no
KP> parameter is passed.

If YourApp::OnInit() calls wxApp::OnInit() parsing is done inside wxApp
itself and uses OnInitCmdLine() and OnCmdLine{Help/Parsed/Error}() virtual
methods to allow you to customize it.

So:

1. Set up wxCmdLineParser to accept the parameters/options you support in
YourApp::OnInitCmdLine()

2. Also override at least YourApp::OnCmdLineParsed() to get the parsing
results.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Kenneth Porter
2007-03-11 14:45:47 UTC
Permalink
Post by Vadim Zeitlin
If YourApp::OnInit() calls wxApp::OnInit() parsing is done inside
wxApp
itself and uses OnInitCmdLine() and OnCmdLine{Help/Parsed/Error}()
virtual methods to allow you to customize it.
Thanks! I'd read through all the wx sources and headers for the
application and cmdline classes and figured out how the overrides work
and when they're called. I came up with the following simple override for
my needs. The one thing I found confusing was that SetDesc seems to
append to the existing options, not replace them. The name would suggest
replacement. (There are some default command line options installed in
the application base classes.)

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

static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{
wxCMD_LINE_PARAM,
wxEmptyString,
wxEmptyString,
gettext_noop("select a configuration"),
wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL
},

// terminator
{
wxCMD_LINE_NONE,
wxEmptyString,
wxEmptyString,
wxEmptyString,
wxCMD_LINE_VAL_NONE,
0x0
}
};

parser.SetDesc(cmdLineDesc);

}
chris elliott
2007-03-12 16:02:23 UTC
Permalink
http://wxhatch.cvs.sourceforge.net/wxhatch/wxhatch/wxhatch.cpp?revision=1.578&view=markup&pathrev=MAIN

look at MyApp::OnInit

chris
Post by Kenneth Porter
Can anyone point me to some code that illustrates proper use of the command
line parsing machinery? I looked through the samples directory and only see
one very tepid use of it, which doesn't call the base wxApp::OnInit method.
How do I invoke that and still use the parser system?
For now my initial app needs a parameter to indicate which of several
sections in a configuration file should be used, eg. invocation would be
"myapp config1" or "myapp config2", with a default config assumed if no
parameter is passed.
---------------------------------------------------------------------
Loading...