Skip to content

Lightweight c++ options parser

October 13, 2014

Using the new C++11 regular expressions, command line option parsing becomes trivial. I wrote a lightweight command line options parser because I was unsatisfied with all of the other libraries out there. They either do not support the syntax that I like, or they are bloated, trying to do too much.

There are two pieces of syntax that need to be parsed in specifying command line options:

  • the specification of which options to recognise, and
  • the command line options themselves.

To recognise these, we have two regular expressions. For the first, we want to be able to write something like "f,file", to specify a short and long option, where either are optional. For the second, we want to recognise long options, with or without =, and short options that can be grouped together, with the last option being able to take a parameter.

The two regular expressions are:

    std::basic_regex<char> option_matcher
      ("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([[:alpha:]]+)");

    std::basic_regex<char> option_specifier
      ("(([[:alpha:]]),)?([[:alpha:]][-_[:alpha:]]+)");

Then the rest of the code is there to loop through the options, ask the regex to match them, and store them somewhere.

The library can be found at http://github.com/jarro2783/cxxopts.

Advertisements

From → Uncategorized

4 Comments
  1. bilbothegravatar permalink

    The usage looks extremely similar to Boost.Program_options (http://www.boost.org/doc/libs/1_56_0/doc/html/program_options/tutorial.html#idp344363808) … any reason why BPO didn’t cut it for you? Cheers.

    • The basic usage was inspired by Boost PO. I don’t like Boost basically because I think it is bloated. It does more than just command line options, and it seems like seems like a lot to pull in just to parse some command line options. I have also had no end of problems in the last 5 years trying to get autoconf and cmake to recognise the boost libraries without doing something wrong. I finally decided I should just write my own.

  2. Do you mean to say Boost.PO does a lot more than _, or that the whole of the Boost Library collection does a lot more than _?

    Oh, but it’s certainly nice that your library is header only, which Boost.PO unfortunately is not.

  3. I meant that Boost.PO does more than_. It has all this extra stuff for custom parsers and parsing files and so on. All of which I’m sure is very nice, but often you just want a basic command line parser. You #include program options and you get I don’t know how many boost headers. Then you have to link in this huge library on top of that. Plus getting it installed can be painful, whether you compile the whole thing or just the libraries that you use, which can be even more painful.

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

%d bloggers like this: