A magic getopt
daemonology.net
daemonology.net
I've been working on libargs for the past year; it's a declarative argument-parsing library for C: https://github.com/mcinglis/libargs
The main idea is that each argument is by default parsed and stored as a string, or you can optionally specify a function of the type `void f(char * name, char * arg, void * dest)` to parse the argument string and store it in a well-typed destination. This way, you can have an `int` argument by passing `int__argparse` as the parser, and if the user passes a value outside the range of `int`, then an appropriate out-of-range error is printed to the console. Similarly with `uchar__argparse` or something like `point__argparse` (e.g. taking some format like `{x,y}`).
libargs is quite flexible and has worked well for me so far. Automatic help text generation can be added in future while maintaining (non-ABI) backwards compatibility.
The main disadvantage is that it depends on other libraries I've developed that are essentially Jinja-templated C source files that function as makeshift generic types / typeclasses in C. Your inclination towards this approach depends on taste; personally I much prefer deferring the pain to the build system, as opposed to the source code.
Okay, but why?
Ask the FSF how long it took them to finally sort out all the legal issues and prepare new forms.
CLA to non-profit > CLA to indidual > CLA to company
>All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
I doubt the library is usable in C++ though, since longjmp doesn't play well with the destruction of local objects.
That's one of the reasons I use it, actually. For simple utilities it may be adequate to set flags and store values for command-line parameters, but sometimes you need the flexibility of being able to run whatever code you need when an option arrives.
If you are using C++, Boost.Program Options is a solid choice. It is one of the relatively few command line argument parsers in the world which supports close to a full set of (GNU-like) behaviors without custom workarounds. It also enables type safety in the sense of disallowing decimals where integers were expected.
> CGI programs should accept these as command-line options, and also if given as the PATH_INFO; for instance, visiting ‘http://example.org/p.cgi/--help’ in a browser should output the same information as invoking ‘p.cgi --help’ from the command line.
I've been using it quite a bit, to transform a three-liner awk program into a full fledged command in little effort. Very nice to share with colleagues.
Second, with groper each module is free to define its own options. You no longer need to have a centralized place where all the options are defined, and you don't need to do the plumbing through your application to give the right values to the right modules. This way your server.py can say "I want a host and a port" and your logger.py can say "I want the verbosity level and the filename" and the two don't have to know about each other (but can).
Third and most important, groper supports config files, which no other argument parser does. Chances are that if you are creating something more than just a simple command line program, you'd have too many options to specify on the command line. Instead, it'd be lovely to have a config file, and be able to override some of the options via command line args. Python's ConfigParser is a mess, and it's a mess that works completely differently from argparse/optparse/getopt. I also don't consider something like config.py to be good practice either.
So basically, groper is your one stop shop for getting config files and command line options right. Oh, and it can generate sample config files for you if you want.
Also, better errors for when the programmer screws up the formatting of the doc string.
For example:
ls -la * .txt
OK
ls * .txt -la
Not OK
This drives me mad.
After a glob, of say, * .txt, and let's say there are a.txt, b.txt and c.txt, if I call
ls * .txt -ltra
it expands to ls a.txt b.txt c.txt -ltra
In POSIX it expects options to appear before files, see: https://en.wikipedia.org/wiki/Getopt#Example_1_.28using_POSI...
Trust me, I wish this were not adhered to, but it is.