GETARG(3)
๐ GETARG(3) ๐
okay bestie, gather round, because we're cracking open the Library Functions Manual and this page is literally called GETARG(3) and it's about collecting command line options and honestly? it's giving "I read the source once and now I'm unstoppable." let's go โจ
๐ธ NAME ๐ธ
getarg, arg_printusage โ collect command line options
๐ SYNOPSIS ๐
#include <getarg.h>
int
getarg(struct getargs *args, size_t num_args, int argc, char **argv,
int *optind);
void
arg_printusage(struct getargs *args, size_t num_args,
const char *progname, const char *extra_string);
๐ฆ DESCRIPTION ๐ฆ
getarg() collects any command line options given to a program in an easily used way. arg_printusage() pretty-prints the available options, with a short help text. like, it prints them pretty. aesthetic. we love that for us ๐
args is the option specification to use, and it's an array of struct getargs elements. num_args is the size of args (in elements). argc and argv are the argument count and argument vector to extract option from. optind is a pointer to an integer where the index to the last processed argument is stored, it must be initialised to the first index (minus one) to process (normally 0) before the first call. yes bestie, minus one, do not skip that part, I am begging ๐
arg_printusage take the same args and num_args as getarg; progname is the name of the program (to be used in the help text), and extra_string is a string to print after the actual options to indicate more arguments. The usefulness of this function is realised only be people who has used programs that has help strings that doesn't match what the code does. ๐ญ (it's so real. it's SO real.)
The getargs struct has the following elements. not me having a whole personality about a struct:
struct getargs{
const char *long_name;
char short_name;
enum { arg_integer,
arg_string,
arg_flag,
arg_negative_flag,
arg_strings,
arg_double,
arg_collect
} type;
void *value;
const char *help;
const char *arg_help;
};
long_nameis the long name of the option, it can beNULL, if you don't want a long name.short_nameis the characted to use as short option, it can be zero. (yes, "characted". we're keeping it. it's iconic.)- If the option has a value the
valuefield gets filled in with that value interpreted as specified by thetypefield. helpis a longer help string for the option as a whole, if it'sNULLthe help text for the option is omitted (but it's still displayed in the synopsis).arg_helpis a description of the argument, ifNULLa default value will be used, depending on the type of the option:arg_integerโ the argument is a signed integer, andvalueshould point to anint.arg_stringโ the argument is a string, andvalueshould point to achar*.arg_flagโ the argument is a flag, andvalueshould point to anint. It gets filled in with either zero or one, depending on how the option is given, the normal case being one. Note that if the option isn't given, the value isn't altered, so it should be initialised to some useful default. ๐จ bestie this is the classic gotcha, initialise your default, do not leave it vibing in the void.arg_negative_flagโ this is the same asarg_flagbut it reverses the meaning of the flag (a given short option clears the flag), and the synopsis of a long option is negated.arg_stringsโ the argument can be given multiple times, and the values are collected in an array;valueshould be a pointer to astruct getarg_stringsstructure, which holds a length and a string pointer.arg_doubleโ argument is a double precision floating point value, andvalueshould point to adouble.arg_collectโ allows more fine-grained control of the option parsing process.valueshould be a pointer to agetarg_collect_infostructure:
typedef int (*getarg_collect_func)(int short_opt,
int argc,
char **argv,
int *optind,
int *optarg,
void *data);
typedef struct getarg_collect_info {
getarg_collect_func func;
void *data;
} getarg_collect_info;
With the func member set to a function to call, and data to some application specific data. The parameters to the collect function are:
short_flagโ non-zero if this call is via a short option flag, zero otherwiseargc,argvโ the whole argument listoptindโ pointer to the index inargvwhere the flag isoptargโ pointer to the index inargv[*optind]where the flag name startsdataโ application specific data
You can modify *optind, and *optarg, but to do this correct you (more or less) have to know about the inner workings of getarg. (iykyk ๐ฎ)
You can skip parts of arguments by increasing *optarg (you could implement the -z3 set of flags from gzip with this), or whole argument strings by increasing *optind (let's say you want a flag -c x y z to specify a coordinate); if you also have to set *optarg to a sane value.
The collect function should return one of ARG_ERR_NO_MATCH, ARG_ERR_BAD_ARG, ARG_ERR_NO_ARG, ENOMEM on error, zero otherwise.
For your convenience there is a function, getarg_optarg(), that returns the traditional argument string, and you pass it all arguments, sans data, that where given to the collection function.
๐ซถ Don't use this more this unless you absolutely have to.
Option parsing is similar to what getopt uses. Short options without arguments can be compressed (-xyz is the same as -x -y -z), and short options with arguments take these as either the rest of the argv-string or as the next option (-ofoo, or -o foo). slay, compressed flags, we stan a compact queen ๐
Long option names are prefixed with -- (double dash), and the value with a = (equal), --foo=bar. Long option flags can either be specified as they are (--help), or with an (boolean parsable) option (--help=yes, --help=true, or similar), or they can also be negated (--no-help is the same as --help=no), and if you're really confused you can do it multiple times (--no-no-help=false, or even --no-no-help=maybe). ๐ญ --no-no-help=maybe is genuinely the most unhinged thing on this entire page and I'm obsessed.
๐ EXAMPLE ๐
#include <stdio.h>
#include <string.h>
#include <getarg.h>
char *source = "Ouagadougou";
char *destination;
int weight;
int include_catalog = 1;
int help_flag;
struct getargs args[] = {
{ "source", 's', arg_string, &source,
"source of shippment", "city" },
{ "destination", 'd', arg_string, &destination,
"destination of shippment", "city" },
{ "weight", 'w', arg_integer, &weight,
"weight of shippment", "tons" },
{ "catalog", 'c', arg_negative_flag, &include_catalog,
"include product catalog" },
{ "help", 'h', arg_flag, &help_flag }
};
int num_args = sizeof(args) / sizeof(args[0]); /* number of elements in args */
const char *progname = "ship++";
int
main(int argc, char **argv)
{
int optind = 0;
if (getarg(args, num_args, argc, argv, &optind)) {
arg_printusage(args, num_args, progname, "stuff...");
exit (1);
}
if (help_flag) {
arg_printusage(args, num_args, progname, "stuff...");
exit (0);
}
if (destination == NULL) {
fprintf(stderr, "%s: must specify destination\n", progname);
exit(1);
}
if (strcmp(source, destination) == 0) {
fprintf(stderr, "%s: destination must be different from source\n");
exit(1);
}
/* include more stuff here ... */
exit(2);
}
The output help output from this program looks like this:
$ ship++ --help
Usage: ship++ [--source=city] [-s city] [--destination=city] [-d city]
[--weight=tons] [-w tons] [--no-catalog] [-c] [--help] [-h] stuff...
-s city, --source=city source of shippment
-d city, --destination=city destination of shippment
-w tons, --weight=tons weight of shippment
-c, --no-catalog include product catalog
look at that alignment. chef's kiss. the help string matches what the code does. a miracle ๐
๐ BUGS ๐
okay this is the part where the manpage is brutally honest and honestly? relatable queen.
- It should be more flexible, so it would be possible to use other more complicated option syntaxes, such as what
ps(1), andtar(1), uses, or the AFS model where you can skip the flag names as long as the options come in the correct order. - Options with multiple arguments should be handled better.
- Should be integrated with SL.
- It's very confusing that the struct you pass in is called
getargS. ๐ญ fr, why is it plural. why.
๐ SEE ALSO ๐
and that's the whole page bestie, no cap, we kept every single flag, every struct member, every --no-no-help=maybe. this one came from ROKEN and it's dated September 24, 1999, and it lives in the GETARG(3) manual โ so go forth, parse those options, and remember: initialise optind to the first index minus one, and set your flag defaults before you call. periodt ๐
๐ซถโจ
๐ฆ where this page comes from
- alpine 3.22: heimdal-doc 7.8.0-r4 ยท BSD-3-Clause
- alpine 3.23: heimdal-doc 7.8.0-r5 ยท BSD-3-Clause
- alpine 3.24: heimdal-doc 7.8.0-r5 ยท BSD-3-Clause
- debian bookworm: heimdal-dev 7.8.git20221117.28daf24+dfsg-2 ยท BSD-3-Clause
- debian trixie: heimdal-dev 7.8.git20221117.28daf24+dfsg-9+deb13u1 ยท BSD-3-Clause
- epel 10: heimdal-devel 7.8.0-26.el10_2 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- epel 9: heimdal-devel 7.8.0-5.el9 ยท BSD and MIT
- fedora 43: heimdal-devel 7.8.0-26.fc43 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- fedora 43: heimdal-devel 7.8.0-26.fc43 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- fedora 44: heimdal-devel 7.8.0-26.fc43 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- fedora 44: heimdal-devel 7.8.0-26.fc43 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- fedora 44: heimdal-devel 7.8.0-28.fc44 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- fedora 44: heimdal-devel 7.8.0-28.fc44 ยท BSD-2-Clause AND BSD-3-Clause AND HPND-export-US AND HPND-export2-US AND LicenseRef-Fedora-Public-Domain
- nixpkgs unstable: heimdal 7.8.0-unstable-2024-09-10
- ubuntu jammy: heimdal-dev 7.7.0+dfsg-3ubuntu1 ยท BSD-3-Clause
- ubuntu noble: heimdal-dev 7.8.git20221117.28daf24+dfsg-5ubuntu3 ยท BSD-3-Clause
- ubuntu questing: heimdal-dev 7.8.git20221117.28daf24+dfsg-9ubuntu1
- ubuntu resolute: heimdal-dev 7.8.git20221117.28daf24+dfsg-11ubuntu1 ยท BSD-3-Clause
- ubuntu stonking: heimdal-dev 7.8.git20240113.1b4565a+dfsg-2ubuntu1 ยท BSD-3-Clause
- Copyright (c) 1999 - 2002 Kungliga Tekniska Hรถgskolan
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms