Parsing the Command Line

team bbl


Passing commands to the application to be read on initialization is often useful, and if the application is document-oriented, you should allow files to be loaded in this way. You also might want to let the application be run from the operating system command line, for example to automate tasks from a makefile, in which case command-line options can be used to tell the application that it should be run without a user interface. Although most configuration of an application will be done via the user interface, command-line configuration options can be appropriate in some cases, such as turning on a debug mode.

wxWidgets provides the wxCmdLineParser class to make this programming task quite easy, avoiding the need to process wxApp::argc and wxApp::argv directly. The class looks for switches (such as -verbose), options (such as-debug:1), and parameters (such as "myfile.txt"). It can recognize both short and long forms of switches and options, and each item can have a help string, which will be used by Usage when writing help text to the current log target.

Here's an example, showing how to parse switches, options, and parameters:

[View full width]

#include "wx/cmdline.h" static const wxCmdLineEntryDesc g_cmdLineDesc[] = { { wxCMD_LINE_SWITCH, wxT("h"), wxT("help"), wxT("displays help on the command line parameters") }, { wxCMD_LINE_SWITCH, wxT("v"), wxT("version"), wxT("print version") }, { wxCMD_LINE_OPTION, wxT("d"), wxT("debug"), wxT("specify a debug level") }, { wxCMD_LINE_PARAM, NULL, NULL, wxT("input file"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_NONE } }; bool MyApp::OnInit() { // Parse command line wxString cmdFilename; wxCmdLineParser cmdParser(g_cmdLineDesc, argc, argv); int res; { wxLogNull log; // Pass false to suppress auto Usage() message res = cmdParser.Parse(false); } // Check if the user asked for command-line help if (res == -1 || res > 0 || cmdParser.Found(wxT("h"))) { cmdParser.Usage(); return false; } // Check if the user asked for the version if (cmdParser.Found(wxT("v"))) { #ifndef __WXMSW__ wxLog::SetActiveTarget(new wxLogStderr); #endif wxString msg; wxString date(wxString::FromAscii(__DATE__)); msg.Printf(wxT("Anthemion DialogBlocks, (c) Julian Smart, 2005 Version %.2f, %s"), wbVERSION_NUMBER, (const wxChar*) date); wxLogMessage(msg); return false; } // Check for debug level long debugLevel = 0; if (cmdParser.Found(wxT("d"), & debugLevel)) { } // Check for a project filename if (cmdParser.GetParamCount() > 0) { cmdFilename = cmdParser.GetParam(0); // Under Windows when invoking via a document // in Explorer, we are passed the short form. // So normalize and make the long form. wxFileName fName(cmdFilename); fName.Normalize(wxPATH_NORM_LONG|wxPATH_NORM_DOTS| wxPATH_NORM_TILDE|wxPATH_NORM_ABSOLUTE); cmdFilename = fName.GetFullPath(); } ... return true; }

The use of wxFileName for normalizing the file name is necessary because Windows sometimes passes the short form when the application is invoked from the command line.

As we noted earlier in the chapter, Mac OS X doesn't use the command line when running an application by opening an associated document; instead, wxApp::MacOpenFile is called after the application has initialized. However, the command-line method is used by other operating systems. If you intend to write a document-based application for Mac OS X and other operating systems, you should allow for both methods.

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net