8 ms·
This is really cool (I've been wondering how to do a string-switch for a while now), but I don't think getopt is a great use case, because getopt still results
by majika 11y ago
This is really cool (I've been wondering how to do a string-switch for a while now), but I don't think getopt is a great use case, because getopt still results in imperative argument parsing and this always results in pain.
I've been working on libargs for the past year; it's a declarative argument-parsing library for C: https://github.com/mcinglis/libargs 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.
- voltagex_ 11y ago>To accept notable contributions, I'll require you to assign your copyright to me. Okay, but why?
- oakwhiz 11y agoAmong other things, it allows the project owner to relicense the project without having to contact every contributor requesting permission to relicense.
- Tomte 11y agoAnd usually makes accepting contributions from people outside the US a mess. Ask the FSF how long it took them to finally sort out all the legal issues and prepare new forms.
- skrebbel 11y agoWhy would a Canadian programmer have trouble accepting contributions from outside the US, specifically?
- detaro 11y agoNot specifically outside the US, but the US (and apparently Canada) have the concept of copyright assignment, many other countries don't, so it gets interesting what value and consequences a copyright assignment by a programmer from there has, esp. in local courts and when the contributor isn't on your side.
- rwmj 11y agoDon't forget that "moral rights" may not be assigned at all, in at least some countries, so you'll need a per-country agreement that the contributor won't enforce them ... in countries where it is possible to agree not to enforce them, which is not all countries. https://en.wikipedia.org/wiki/Moral_rights#In_Europe https://en.wikipedia.org/wiki/Moral_rights#In_Europe
- vog 11y agoAlso, people are generally more inclined to sign that for a non-profit organization like FSF or the OSGeo Foundation, than for an individual person, although that's still more accepted than signing to a company. CLA to non-profit > CLA to indidual > CLA to company
- ZaoFishbones 11y agoFun fact, C++ reserves double-underscores anywhere in names for the implementation. It's highly unlikely that you'll run into anything colliding with your names in the wild, but if someone wants to use your library in C++, it's technically bogus.
- Kristine1975 11y agoIt's similar in C. C11 Standard chapter 7.1.3: >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.
- cperciva 11y agogetopt still results in imperative argument parsing 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.