Introduction
Many command-line tools need to parse command-line arguments. In C, one of the most widely used functions for this purpose is getopt() and its GNU extension getopt_long(). However, these functions have two major issues. First, they are not portable. getopt is part of the POSIX standard but not the C standard; getopt_long is not part of any standards. In addition, getopt may behave differently depending on whether GNU extension is enabled. Using these functions can be tricky. Second, both functions rely on global variables, which may interfere with more complex use cases (e.g. sub-commands).
These limitations motivated the development of several other argument parsing libraries. While these libraries often have cleaner APIs and more functionality, most of them lack some getopt_long features. This blog post reviews several argument parsing libraries in C/C++ and introduces my own getopt replacement at the end.
Argument parsing libraries in C/C++
The following table lists common features in argument parsing libraries. Stars indicates getopt_long features.
Feature | Explanation |
---|---|
post* | Parse options after non-option/positional arguments |
compact* | When appropriate, “-a -b foo” can be written as “-abfoo” |
mulocc* | Keep track of an option occurring multiple times |
order* | Keep track of the order of options |
oparg* | A long option may optionally take an argument |
type | Built-in argument type checking and parsing |
fmt | Print formatted help messages |
wchar | Support multi-byte characters |
The table below shows the feature sets of several command-line argument parsing libraries. Only libraries supporting both short and long options are considered (stars indicate 1- or 2-file libraries):
library | lang | post | compact | mulocc | order | oparg | type | fmt | wchar |
---|---|---|---|---|---|---|---|---|---|
getopt_long | C/C++ | Y | Y | Y | Y | Y | N | N | maybe |
argh* | C++11 | semi | N | N | N | N | N | N | ? |
argp | C/C++ | Y | Y | Y | Y | ? | N | Y | ? |
argparse* | C/C++ | Y | Y | N | N | ? | Y | Y | ? |
args* | C++11 | Y | Y | Y | N | ? | Y | Y | ? |
argtable* | C/C++ | Y | Y | Y | N | ? | Y | Y | ? |
cxxopts* | C++11 | Y | Y | Y | N | ? | Y | Y | ? |
CLI11 | C++11 | Y | Y | switch | N | N | Y | Y | ? |
gopt* | C/C++ | Y | Y | switch | N | Y | N | N | N |
ketopt* | C/C++ | Y | Y | Y | Y | Y | N | N | N |
tclap | C++ | ? | N | N | N | ? | Y | Y | ? |
Notably, many libraries discard the relative order between options, arguably the least important getopt feature. They often add type checking and automatic help message formatting. I think type checking comes in handy, but message formatting is not as valuable because I prefer my own format over theirs.
The list in the table is of course incomplete. Some important ones that are missing include Boost’s Program_options and Google’s gflags, both of which are much heavier libraries. I haven’t spent enough time on them. If you have relevant information on them or your favorite library that is missing, or you think the table is wrong, please help me to improve it. Thanks in advance!
Ketopt: my single-header argument parsing library
I occasionally care about the order of options, a feature missing from most non-getopt libraries (argp has it but is not portable). In the end, I developed my own library ketopt (examples here, including one on sub-command). It is implemented in ANSI C and doesn’t invoke heap allocations. Ketopt has a similar API to getopt_long except that 1) ketopt doesn’t use any global variables and 2) ketopt has an explicit function argument to allow options placed after non-option arguments. Developers who are familiar with getopt_long should be able to learn ketopt quickly.
Conclusions
Command-line argument parsing is relatively simple (ketopt has <100 LOCs), but implementing it by yourself is tricky, in particular if you want to match the features in getopt_long. My ketopt is largely a portable getopt_long without global variables. In addition to mine, you may consider gopt in C. It is small, easy to use and supports key getopt_long features. For C++ programmers, cxxopts is a decent choice. It is feature rich, close to getopt_long, and has similar APIs to Boost’s Program_options and Python’s argparse.
I strongly discourage the use of libraries deviating too much from getopt (e.g. argh and tclap). Most end users expect getopt behaviors. When your tool acts differently, it will confuse users. Command-line interface is one the first things users experience. Please get it right.
Couple of quick notes about CLI11: It does have a way to get argument order, and long arguments (generalized flags) can optionally take an argument as of 1.8. Also, it comes in a one-file version as well.
One should also mention Qt 5 (>= 5.2) with its class QCommandLineParser. I haven’t checked if GTK has similar classes.
I assume CLARA might be worth looking at, https://github.com/catchorg/Clara
I would like to share the library I’ve created for C++11:
https://github.com/saveman/octargs