summaryrefslogtreecommitdiffstats
path: root/src/foodopts.c
blob: 338b97e7d542c2388ca3229e392058017f010799 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include "foodopts.h"

int
get_foodopt(int argc, char *const argv[],
            const char *optstring,
            const struct foodoption *longopts,
            int *longindex)
{
  return getopt_long(argc, argv, optstring,
                     (struct option *)longopts, longindex);
}

const char *
get_argument(const char opt,
             const struct foodoption *longopts)
{
  int i = 0;
  while ((longopts[i].name)
         && (longopts[i].val)) {
    if (longopts[i].val == opt)
      return longopts[i].arg;
    i++;
  }
  return NULL;
}

void
foodopt_help(char * argv0,
             const struct foodoption *longopts)
{
  fprintf(stderr, "%s [OPTION ...] FILE ...\n", argv0);
  fprintf(stderr, "\nOPTIONS:\n");


  int i = 0;
  while ((longopts[i].name)
         && (longopts[i].val)) {
    if (longopts[i].category) {
      fprintf(stderr, "\n%s:\n", longopts[i].category);
    }
    fprintf(stderr, "-%c, --%s%s%s: %s\n",
            longopts[i].val,
            longopts[i].name,
            (longopts[i].has_arg == required_argument || longopts[i].has_arg == optional_argument) ? " " : "",
            (longopts[i].has_arg == required_argument || longopts[i].has_arg == optional_argument) ? longopts[i].arg : "",
            longopts[i].help);
    i++;
  }
}