Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Code Block
languagecpp
//---------------------------------------------------------------------------
// 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.

...

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:

Code Block
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.

...

Tada! So we now have tracing enabled. So next we are going to add code to do tracing in the file, edit main.cpp to this:

...

Code Block
languagecpp
//---------------------------------------------------------------------------
// 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>

#include <COL/COLlog.h>
COL_LOG_MODULE;

int main(int argc, const char** argv) {
   try{
      CMDlineParser Parser;
      Parser.parseArgs(argc, argv);
      COL_TRC("Parsed " << argc << " arguments");
      COL_DBG("This is really detailed tracing.");
      COL_VAR(argc);
      
      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;
}

See how lines 20 and 21 were added:

Code Block
languagecpp
#include <COL/COLlog.h>
COL_LOG_MODULE;

and lines 27-29:

Code Block
      COL_TRC("Parsed " << argc << " arguments");
      COL_DBG("This is really detailed tracing.");
      COL_VAR(argc);

Now we can recompile the program and run it with tracing on:

...

You can observe the different levels of tracing in the output.

Next let’s add some logic to define two flags, one with a parameter, one without.

...

Add some tracing

...

So we edit the main.cpp file to look like this:

Code Block
languagecpp
//---------------------------------------------------------------------------
// 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>

#include <COL/COLlog.h>
COL_LOG_MODULE;

int main(int argc, const char** argv) {
   try{
      CMDlineParser Parser;
      Parser.addFlagWithoutArgument("run", "Run this thing");
      Parser.addFlagWithArgument("file", "name", "Write data to this file");
    
      Parser.parseArgs(argc, argv);
      COL_TRC("Parsed " << argc << " arguments");
      COL_DBG("This is really detailed tracing.");
      COL_VAR(argc);
      
      if (Parser.parsingErrorsPresent(COLcout)){
         Parser.showUsage(COLcout);
         return EXIT_FAILURE;
      }

      if (Parser.isFlagPresent("run")){
         COLcout << "Okay run for the hills!" << newline;
      }

      COLstring File = "default";
      if (Parser.isFlagPresent("file")){
         Parser.flagArgument("file", File);
      }
      COLcout << "File:" << File << 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;
}

We can test out the new logic by compiling and running the program with these command lines arguments:

...

And we are done. We have learned how to:

  • Add a library (CMD) to our project

  • How to use that library to create command line parameters

  • And how to add tracing to our application.

Next we can see how we can add in the FIL library which has a number of file handling functions.