3

I'm working on a framework and I've been using TCLAP to handle command-line options. When the program help is called, I would like to print its parameter usage automatically grouped in categories. An output example would be:

$ ./program help

Learning Options
    -t          Train mode
    -neg        Specifies the path for the negative examples
    -pos        Specifies the path for the positive examples

Detection Options
    -d          Detection mode
    -param      Parameters file path
    -o          Output folder
    -track      Enables tracking

Feature Extraction Options
    -w          Window size
    -feat       Feature type. Available: SIFT and SURF.

I've been looking in the TCLAP's documentation, but didn't find anything. I'm still looking into this StackOverflow post. I've found that libobj seems to do that, but it is not entirely clear.

How can I do that with TCLAP? If it is not possible, is there another library which I can use?

Bonus - A small library, such as TCLAP :-)

Community
  • 1
  • 1
Yamaneko
  • 3,255
  • 2
  • 33
  • 56

2 Answers2

4

is there another library which I can use?

You can use Boost.Program_options library. It is relatively small (370k on Fedora 17).

An example can be found here:

namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>(), "set compression level")
;

po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);    

if (vm.count("help")) {
    cout << desc << "\n";
    return 1;
}

if (vm.count("compression")) {
    cout << "Compression level was set to " 
 << vm["compression"].as<int>() << ".\n";
} else {
    cout << "Compression level was not set.\n";
}
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
BЈовић
  • 59,719
  • 40
  • 167
  • 261
  • 1
    370 KiB is bigger than most of the programs I'd be using it with. – Jonathan Leffler Oct 09 '12 at 07:02
  • @JonathanLeffler It depends. On modern PCs, 370k is nothing. On embedded, it might not fit the memory. – BЈовић Oct 09 '12 at 07:56
  • 2
    @JonathanLeffler BTW if you are on embedded platform, program parameters (if any) should be passed in the simplest form. You do not need a library to parse input parameters. – BЈовић Oct 09 '12 at 18:56
  • @BЈовић Boost does exactly what I want. But, I'll have to wait a few days in case someone comes up with a solution using a small library. – Yamaneko Oct 10 '12 at 22:23
2

You could use Gengetopt and the getopt library. It includes a text command that will allow you to insert the grouping headers. A nice thing is that this support the GNU conventions for command line args, which many people know.

Gene
  • 44,980
  • 4
  • 53
  • 91