.TL Testing Argp .AU tropf .AB presentation of short helper to more easily test argp .AE .DA . .NH 1 Intro . .NH 2 Argp .PP .URL "https://www.gnu.org/software/libc/manual/html_node/Argp.html" "Argp" is one of the more advanced parsers for command-line arguments out there. As it is part of glibc, it is available on all of the major distributions. . .PP You only see it rarely, as most applications don't care too much about the output of .CW "--help" or move large parts of it into their man page. And as Argp is a C-Library most C++-devs don't fancy it too much. . .PP Once set up it's usage is very straight forward, just pass .CW "argc" and .CW "argv" directly into Argp and everything will be handled. . .NH 2 Testing .PP When building applications I often spend a large part of my time checking that the configuration works properly. Part of that is testing that command-line arguments are parsed correctly and are associated to their internal variables. . .PP Argp, however, has fairly high (and strange) demands for its inputs. First of all, it requires the typical format of .CW argc and .CW argv : While the former is just an integer, the latter is an array of pointers to c-style strings, i.e. null-terminated char arrays. Also the memory that .CW argv is pointing to might be overwritten during Argp's operation, so it has to be re-initialized for each test case. Finally, the memory .B "must not" be on the heap. This means, argv may only point to regions of the stack, otherwise there will be .CW "malloc" -errors. . .NH 1 Concept .PP The helper class below accepts a string. This will be exploded into a list of words. From there, all words will be stored consecutively separated by null bytes. .NOTE .B "Realtabs are not regarded as separator!" .PP All data resides on the stack, thus there is a limit for the total number of characters stored (4096 bytes including separator bytes) and a maximum number of arguments (31). . .NH 1 Code .PP For usage please have a look at the test code. .NOTE .CW argv[0] has to be intialized with the binary name when using w/ argp. . .NH 2 License .PP This code is licensed under .URL "http://creativecommons.org/publicdomain/zero/1.0/" "CC0" (public domain). . .NH 2 Full Text .PP .URL "../blob/argv_helper.cc" "download" .NOTE Due to fun quirks of groff, any .CW "\(aq" in the code will be displayed as .CW ' (and produces a syntax error when compiled). Just download the files directly. . .CB .so ../blob/argv_helper.cc .CE . .NH 2 Tests .PP .URL "../blob/argv_helper_test.cc" "download" .CB .so ../blob/argv_helper_test.cc .CE . .NH 1 See also .PP .ULS .LI .URL "https://www.gnu.org/software/libc/manual/html_node/Argp.html" "Argp doc" . .LI .URL "https://nongnu.askapache.com/argpbook/step-by-step-into-argp.pdf" "Step by step into Argp" ":" best guide for Argp I know; .URL "http://www.nongnu.org/argpbook/" "project homepage" . .LI .URL "https://smplxmpp.com/" "smplxmpp" ":" uses this code . .LI .URL "https://web.njit.edu/~sohna/cs288/hello-memap.pdf" "layout of argv" .ULE