Handling Command Line Parameter using Cpp-optparse

Recently I needed to create tool with lots of command line parameters. Since I don’t want to create my own parser and using boost or getopt is too bothersome to do, I need simpler option. I found some option like optionparser , cpp-optparse and some others. I choose cpp-optparse because I like python optparse, although it deprecated from python since 2.7.

It consist of 1 header file and 1 c++ file, (yes! it’s not header only library). Using this one is pretty straight forward and actually you can refer to python’s optparse documentation. However I don’t like the way this library handle error, it just print error message and quit immediately without throwing any exception.

In my case, I modified OptionParser::error() to throw exception instead of exit, this way I can manage what I need to do from main loop.

    void OptionParser::error(const string& msg) const {
        print_usage(cerr);
        cerr << prog() << ": " << _("error") << ": " << msg << endl;
        throw std::exception(msg.c_str(), 2);
        //exit();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s