Command line arguments in wxWidgets
The wxCmdLineParser documentation is nice enough, but as is so often the case with programming library docs, they don’t provide any decent examples.
We learn by example, so here’s a very short yet fully working program showing how to parse and use command line arguments in your wxWidgets program.
#include<wx/wx.h> #include<wx/cmdline.h> #include<iostream> using namespace std; class cmd : public wxApp { void processArgs(){ wxCmdLineEntryDesc cmdLineDesc[] = { {wxCMD_LINE_OPTION, wxT("f"), wxT("foo"), wxT("Foo factor desired") }, {wxCMD_LINE_PARAM, NULL, NULL, wxT("Files to grault"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, {wxCMD_LINE_SWITCH, wxT("z") }, {wxCMD_LINE_NONE} }; wxCmdLineParser parser (cmdLineDesc, argc, argv); parser.Parse(); // That’s it! Now do stuff with the args: wxString optf; parser.Found( wxT("f"), &optf); } bool OnInit(){ processArgs(); exit(0); } }; IMPLEMENT_APP(cmd)
The “eek“iest looking part of that is the construction of the wxCmdLineEntryDesc array which spans lines 11 to 30.
This is just an array of wxCmdLineEntryDesc structs, each of which specifies some description of a command line option. For example:
{wxCMD_LINE_SWITCH, wxT("z") }
is the simplest of these: it states that a ‘z’ switch can be specified when running the program.
{wxCMD_LINE_OPTION,
wxT("f"),
wxT("foo"),
wxT("Foo factor desired")
}
specifies that an option -f, which can also be written —foo, takes an argument.
The other array on lines 19 to 25 specifies command line parameters – these are any remaining arguments which don’t have switches before them, usually things like filenames.
Most of the options in these structs can be omitted by placing a NULL instead.
The clever thing about the parser is that it will construct a complete “usage” message for your program, simply from the description you provide in these arrays. E.g, if I just run the above program, it automatically constructs the message:
id@betty:~/tmp/wx/cmd> ./cmd Usage: cmd [-f <str>] [-z] Files to grault... -f, --foo=<str> Foo factor desired -z The required parameter 'Files to grault' was not specified.
If I provide some arguments, the following output occurs:
id@betty:~/tmp/wx/cmd> ./cmd -f severe -z text.txt words.txt clogs.txt Number of params: 3 Param 1: text.txt Param 2: words.txt Param 3: clogs.txt Option f: severe Option z? 1
The simplest possible example is below:
#include<wx/wx.h> #include<wx/cmdline.h> #include<iostream> using namespace std; class cmd : public wxApp { bool OnInit(){ wxCmdLineEntryDesc desc[] = { {wxCMD_LINE_OPTION, wxT("f") } }; wxCmdLineParser parser (desc, argc, argv); parser.Parse(); wxString optf; parser.Found( wxT("f"), &optf); exit(0); } }; IMPLEMENT_APP(cmd)
This is compiled with:
g++ simplecmd.cpp `wx-config --cflags --libs` -o simple
More info can be found in the docs, but do note that if you’re going to be using the wxString with conventional C++ / stdlib, you’ll have to use the mb_str() method as I have done above, otherwise you’ll only get the first char of the string. More info on converting to and from wxStrings is in the wxWidgets wiki.