Command Line Parsing with TCLAP

Description: Introduction to command line parsing with TCLAP.

Keywords: ecl command line parsing

Tutorial Level: BEGINNER

Introduction

For a more detailed tutorial, visit the TCLAP documentation. Some common use cases are outlined below.

Initialisation

A single object is used to parse the command line. Initialise it with a few default properties. These will be used to automatically generate a --help argument.

   1 // Supply a program description, argument separator (optional) and version number (optional).
   2 CmdLine cmd("This is a test program to test the command line parsing facilities provided by TCLAP.");
   3 // CmdLine cmd("This is a test program to test the command line parsing facilities provided by TCLAP.", ' ', "0.01");
   4 

Adding Arguments

Valid argument types include:

  • ecl::SwitchArg : simple boolean.

  • ecl::ValueArg : expect some value on the command line.

  • ecl::MultiArg : permit multiple values for an argument which get stored in a vector.

  • ecl::MultiSwitchArg : as above, but for booleans

  • ecl::UnlabeledValueArg : does not use a flag (like the arguments for copy), order is important!

  • ecl::UnlabeledMultiArg : reads in all remaining unlabelled args (like rm file1.txt file2.txt etc.)

Toggles/Switches

   1 // Add a boolean (flag, name, description, default)
   2 SwitchArg debug("d","debug","Enable debugging.", false);
   3 cmd.add(debug);

You would call the above program (supposing it has name foo):

> foo -d false

Labelled

   1 // Add a boolean (flag, name, description, default)
   2 SwitchArg debug("d","debug","Enable debugging.", false);
   3 cmd.add(debug);
   4 
   5 // Add an integer 
   6 // (flag,name,description,compulsory flag,default value,type hint")
   7 ValueArg<int> intArg("t","test","An integer argument for testing.",false,5,"integer");
   8 
   9 cmd.add(intArg);

Custom ValueArg types can be used so long as they implement the >> operator.

> foo --test 3 

Unlabelled

These are unlabelled (like what you do with cp my_file_here my_file_there).

   1 // Not a regular option (name,description,compulsory flag,default value,type hint")
   2 UnlabeledValueArg<std::string> outputDirArg("output_dir","Output directory for rectified images.",true,"./rectified","string");
   3 cmd.add(outputDirArg);

> foo /home/snorri/images

Parsing the Arguments

   1 cmd.parse(argc,argv);
   2 bool debug = debugSwitch.getValue();
   3 int test = testArg.getValue();
   4 std::String output_dir = outputDirArg.getValue();

Wiki: ecl_command_line/Tutorials/Command Line Parsing with TCLAP (last edited 2012-01-17 13:58:58 by DanielStonier)