Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The CMD library is a helpful little library which:

  • Makes it easier to add command line arguments to your command line application.

  • Adds in support for the flags that support our tracing system.

  • Also prints out help arguments

So to begin with if you run the HelloWorld application from before with a command line argument like --help you’ll get sweet nothing:

We’ll start by altering the main.cpp file to have some extra lines to use the CMD library:

//---------------------------------------------------------------------------
// Copyright (C) 1997-2020 iNTERFACEWARE Inc.  All Rights Reserved
//
// Module: main.cpp
//
// Description:
//
// An example command line app
//
// Author: Eliot Muir
// Date:   Thu 01/15/2004
//---------------------------------------------------------------------------

#include <COL/COLostream.h>
#include <COL/COLerror.h>
#include <stdlib.h>

#include <CMD/CMDlineParser.h>

int main(int argc, const char** argv) {
   try{
      CMDlineParser Parser;
      Parser.parseArgs(argc, argv);
      if (Parser.parsingErrorsPresent(COLcout)){
         Parser.showUsage(COLcout);
         return EXIT_FAILURE;
      }

      COLcout << "Hello world!" << newline;

      return EXIT_SUCCESS;
   } catch (COLerror& Error) {
      COLcerr << Error.description() << newline;
      return EXIT_FAILURE;
   } catch(...) {
      COLcerr << "Unhandled Exception" << newline;
      return EXIT_FAILURE;
   }
   return EXIT_SUCCESS;
}

Notice the extra #include line with the header file for the CMDlineParser on Line 18 and then we have the extra lines from 22 to 27 which are used to invoke the object to parse the command line arguments for the application.

If we tried to build the command line application without editing the make file we are going to get some linker errors. The following screen shot shows what you might expect (I use “touch” to force make to recompile and link the application by simulating what happens when “main.cpp” is changed:

The missing problem with undefined symbols can be resolved by editing the makefile to include the CMD library so that the build system can compile and link that library into the executable. This is what the makefile needs to be edited into:

BINARY=HelloWorld

SRC=\
main.cpp

MODULES=\
CMD\
COL

include ../makefiles/binary.makefile

See the extra line on line 7? This includes the CMD library. Libraries need to occur in order with the lowest level dependencies shown last. COL is the core library of everything, CMD uses it and so CMD needs to appear before COL in the MODULES variable. The \ at the end of the CMD line is just make’s continuation character.

Now making the binary and running it with --help should show something interesting:

Tada! So we now have tracing enabled. So next we are going to:

  • Add two command line arguments, one with a parameter, one without.

  • Add some tracing

  • Run the executable with the tracing and the command line arguments.

  • No labels