libarguments

libarguments; V 9; 2019-03-18; parsing command line arguments

Synopsis and description

#include <arguments.h>

The library provides functions for parsing command line arguments. It supports standard UNIX flags (-fparameter, -f parameter) and GNU-style arguments (--arg parameter, --arg=parameter).

Argument handling is done through callback model.

Downloading and compiling

GitLab: gitlab.com/Matlib/libarguments
Git clone: https://gitlab.com/Matlib/libarguments.git

Source code and manual pages: Requirements:
  • none
Compiling and installing:
gcc -DSYSLOG_NAMES=1 -Wall -s -fPIC -shared -o/usr/local/lib/libarguments.so arguments.c
cp arguments.h /usr/local/include

The SYSLOG_NAMES definition enables syslog.h built-in facilitynames. It is available in GNU and FreeBSD. On other systems this define has to be removed.

Examples

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <arguments.h>

ARGUMENT_CALLBACK (argument_print)
{
printf ("Printing: \"%s\"\n", parameter);
arg -> value ++;
}

struct Argument args [] =
{
{'s', "string", argument_print, ARGUMENT_F_PAR, NULL},
{'i', "int", argument_int, ARGUMENT_F_PAR | ARGUMENT_F_ONCE, (void *) 2009},
{ }
};

int main (int argc, char **argv)
{
int n;
n = argc - arguments_parse ("foo", "2.4", NULL, "Foo likes moo!", argc, argv, args);
if (n == 0)
arguments_error_freeform ("Input file name is missing.");
if (n > 1)
arguments_error_freeform ("%u file names supplied, only one is needed", n);
printf ("String was printed %d times\n"
"Integer value is %d\n"
"File name is \"%s\"\n"
, (int) (args [0].value), (int) (args [1].value), argv [argc - 1]);
n = open (argv [argc - 1], O_RDONLY);
if (n == -1)
perror (argv [argc - 1]);
return EXIT_SUCCESS;
}