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:
TARGET=HelloWorld DIRS=\ CMD\ COL include ../make/makefile.core
See the extra line on line 5? This includes the CMD library. Libraries did need to occur in order with the lowest level dependencies shown last. Not sure if that applies anymore. COL is the core library of everything, CMD uses it and so CMD needs to appear before COL in the DIRS 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 code to do tracing in the file, edit main.cpp to this:
//--------------------------------------------------------------------------- // 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:
#include <COL/COLlog.h> COL_LOG_MODULE;
and lines 27-29:
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.
So we edit the main.cpp file to look like this:
//--------------------------------------------------------------------------- // 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.