A pet peeve of mine has always been parsing command line arguments – I find it boring and painful. While tools like
getopt offer some degree of help, they don’t offer much of a paradigm shift. I figured it should be possible to abuse the C pre-processor to have it convert a benign list of command line arguments and their type into real parsing code. Since I could not find something similar that already existed; and had a free afternoon; I wrote this: https://github.com/sanjoy/CmdArgs.
README file with the (rather non-rigorous) test code should be enough to get you started. This is not a typical library (since it uses the pre-processor so heavily) – you cannot compile it to, say,
libcmdrargs.so and dynamically link to it. You will always need to include the source in your project.
I have not focused much on code-efficiency; granted that you parse the command line arguments only once, this should not be much of a problem. Bugs, suggestions and flames welcome.