cpp
cpp copied to clipboard
Add recommendations for option parsing
In C++ my experience says:
- If it is okay to use boost, use boost::program_options, it is a solid library.
- It is often not okay to use boost. The headers alone are > 50 MB 🙀 and contain > 13,000 files. And boost::program_options requires linking a precompiled library. This is massive weight and complexity for a build system just to parse options.
- So for lightweight C++ libs that might not otherwise have any dependencies we need to recommend a very lightweight option parsing solution.
/cc @GretaCB @joto @mapbox/core-tech @danpat
libraries to investigate:
- https://github.com/jarro2783/cxxopts
- http://optionparser.sourceforge.net/
- https://github.com/myint/optparse
I usually just use getopt_long() available in libc for simpler cases. The interface is C and not C++ and slightly ugly. But for many cases this is simple enough and doesn't require an extra dependency. If this is not enough it is time to switch to boost::program_options anyway.
Something to investigate: Clara "A simple to use, composable, command line parser for C++ 11 and beyond". By the same guy who created the Catch unit test framework.
Just learned from @kkaefer that mbgl is using https://github.com/Taywee/args for demo apps - which @brunoabinader brought in.
watching progress on Clara at https://github.com/mapbox/vtzero/commit/70fc23278e45788bf4812cb22d888ea024dc9c65. I'm in support for having Clara be our recommendation if @joto's experience is solid this week in this port.
My experience with Clara so far:
Positive:
- Easy to use, clean code.
- Easy to integrate 42k header file.
- In active development.
Negative:
- Documentation is not complete.
- Still in development, changes to be expected.
- Single
-char as parameter (often used to denote stdin) is not possible.
I think it is too early to recommend this as a standard tool, but I think collecting some experience with it is probably a good idea. (That's why I went ahead and used it in the relatively simple vtzero examples.)
I think collecting some experience with it is probably a good idea. (That's why I went ahead and used it in the relatively simple vtzero examples.)
👍 looking forward to learning more as you use it.