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.

  1. #include<wx/wx.h>
  2. #include<wx/cmdline.h>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. class cmd : public wxApp {
  8.  
  9. void processArgs(){
  10.  
  11. wxCmdLineEntryDesc cmdLineDesc[] = {
  12.  
  13. {wxCMD_LINE_OPTION,
  14. wxT("f"),
  15. wxT("foo"),
  16. wxT("Foo factor desired")
  17. },
  18.  
  19. {wxCMD_LINE_PARAM,
  20. NULL,
  21. NULL,
  22. wxT("Files to grault"),
  23. wxCMD_LINE_VAL_STRING,
  24. wxCMD_LINE_PARAM_MULTIPLE
  25. },
  26.  
  27. {wxCMD_LINE_SWITCH, wxT("z") },
  28.  
  29. {wxCMD_LINE_NONE}
  30. };
  31.  
  32. wxCmdLineParser parser (cmdLineDesc, argc, argv);
  33. parser.Parse();
  34.  
  35. // That’s it! Now do stuff with the args:
  36.  
  37. cout << endl << "Number of params: " << parser.GetParamCount() << endl;
  38. cout << "Param 1: " << parser.GetParam(0).mb_str() << endl;
  39. cout << "Param 2: " << parser.GetParam(1).mb_str() << endl;
  40. cout << "Param 3: " << parser.GetParam(2).mb_str() << endl;
  41. wxString optf;
  42. parser.Found( wxT("f"), &optf);
  43. cout << "Option f: " << optf.mb_str() << endl;
  44. cout << "Option z? " << parser.Found( wxT("z") ) << endl;
  45.  
  46. }
  47.  
  48. bool OnInit(){
  49. processArgs();
  50.  
  51. exit(0);
  52. }
  53.  
  54. };
  55.  
  56. IMPLEMENT_APP(cmd)
  57.  

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:

  1. #include<wx/wx.h>
  2. #include<wx/cmdline.h>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. class cmd : public wxApp {
  8.  
  9. bool OnInit(){
  10. wxCmdLineEntryDesc desc[] = { {wxCMD_LINE_OPTION, wxT("f") } };
  11. wxCmdLineParser parser (desc, argc, argv);
  12. parser.Parse();
  13.  
  14. wxString optf;
  15. parser.Found( wxT("f"), &optf);
  16. cout << optf.mb_str() << endl;
  17. exit(0);
  18. }
  19.  
  20. };
  21.  
  22. IMPLEMENT_APP(cmd)
  23.  

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.

No comments

Post a comment


Name:  
Remember personal info?

Email:
URL:
Comment: Emoticons / Textile

This is not a trick question, but a mechanism for spam prevention.
 

  ( Register your username / Log in )

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Powered by Pivot. RSS Feed & ATOM Feed