...
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:
...
To make the video I altering the main.cpp file to have some extra lines to use the CMD library:
Code Block | ||
---|---|---|
| ||
//---------------------------------------------------------------------------
// 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=\
Then to solve the linking issue in the video, I needed to add the CMD dir to the DIRS list: Code Block
Code Block |
---|
TARGET=HelloWorld DIRS=\ CMD\ COL include ../ |
...
make/ |
...
makefile. |
...
core |
See the extra line on line 74? 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 MODULES 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 two command line arguments, one with a parameter, one without.
...
Add some tracing
...
I put in tracing after Parser.parseArgs:
Code Block | ||
---|---|---|
| ||
Parser.parseArgs(argc, argv);
COL_TRC("Parsed " << argc << " arguments");
COL_DBG("This is really detailed tracing.");
COL_VAR(argc); |
And I needed to add this to the includes:
Code Block | ||
---|---|---|
| ||
#include <COL/COLlog.h>
COL_LOG_MODULE; |
Next to add the code to parse command line arguments, there were two blocks of code I had to add:
Code Block | ||
---|---|---|
| ||
CMDlineParser Parser;
Parser.addFlagWithoutArgument("run", "Run this thing");
Parser.addFlagWithArgument("file", "name", "Write data to this file"); |
And:
Code Block | ||
---|---|---|
| ||
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; |
And we are done.