4 ms·
Why are they parsing argv by hand instead of using, say, argp_parse()¹? It’s even the seventh commandment of The Ten Commandments for C Programmers²: “Thou sha
by teddyh 5y ago
Why are they parsing argv by hand instead of using, say, argp_parse()¹? It’s even the seventh commandment of The Ten Commandments for C Programmers²: “Thou shalt study thy libraries and strive not to reinvent them without cause, that thy code may be short and readable and thy days pleasant and productive.”
Also by Henry Spencer, from How to Steal Code³:
“Even widely-available library functions often are not used nearly as much as they should be. A conspicuous example is getopt, for command-line argument parsing. Getopt supplies only quite modest help in parsing the command line, but the standardization and consistency that its use produces is still quite valuable; there are far too many pointless variations in command syntax in the hand-cooked argument parsers in most UNIX programs. Public-domain implementations of getopt have been available for years, and AT&T has published (!) the source for the System V implementation. Yet people continue to write their own argument parsers.”
Now, of course, we have argp_parse(), which also comes included in glibc, and is much better than getopt().
1. https://www.gnu.org/software/libc/manual/html_mono/libc.html#Argp https://www.gnu.org/software/libc/manual/html_mono/libc.html...
2. https://www.lysator.liu.se/c/ten-commandments.html https://www.lysator.liu.se/c/ten-commandments.html
3. https://archive.org/details/inventing-wheel-once https://archive.org/details/inventing-wheel-once
- Arch-TK 5y ago"better" is very subjective. If you just want short options, getopt code is shorter, far more portable and easier to write. Even if you DO want long options, argp is still not portable meaning that unless you only want to support glibc linux, you have to keep a local copy or rewrite or fallback from arpg.
- teddyh 5y agoIf we always limit ourselves to “portable” code, how can we ever progress beyond POSIX?
- Arch-TK 5y agoGenerally it involves writing new code, which can itself be portable and can be reused. I would also note that even POSIX isn't portable in some contexts (such as trying to write code which can run on windows too).