summaryrefslogtreecommitdiffstats
path: root/backend/args.c
blob: 030930deabacbc482cde84013ac65e3f39152dc6 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "args.h"

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>


struct cmdline_args cmdargs;


static void usage(FILE *fd, int argc, char **argv)
{
	fprintf(fd, "Usage: %s [OPTIONS]\n", argv[0]);
	fprintf(fd, "\n");
	fprintf(fd, "  -B|--background             Fork into background\n");
	fprintf(fd, "  -P|--pidfile PATH           Create a PID-file\n");
	fprintf(fd, "  -l|--loglevel LEVEL         Set logging level\n");
	fprintf(fd, "                              0=error, 1=info, 2=debug, 3=verbose\n");
	fprintf(fd, "  -L|--logfile PATH           Write log to file\n");
	fprintf(fd, "  -f|--force                  Force mode\n");
	fprintf(fd, "\n");
	fprintf(fd, "  -h|--help                   Print this help text\n");
}

int parse_commandline(int argc, char **argv)
{
	static struct option long_options[] = {
		{ "help", no_argument, 0, 'h' },
		{ "background", no_argument, 0, 'B' },
		{ "pidfile", required_argument, 0, 'P' },
		{ "loglevel", required_argument, 0, 'l' },
		{ "logfile", required_argument, 0, 'L' },
		{ "force", no_argument, 0, 'f' },
	};
	int c, idx;

	while (1) {
		c = getopt_long(argc, argv, "hBP:l:L:f",
				long_options, &idx);
		if (c == -1)
			break;
		switch (c) {
		case 'h':
			usage(stdout, argc, argv);
			return 1;
		case 'B':
			cmdargs.background = 1;
			break;
		case 'P':
			cmdargs.pidfile = optarg;
			break;
		case 'l':
			if (sscanf(optarg, "%d", &cmdargs.loglevel) != 1) {
				fprintf(stderr, "Failed to parse --loglevel argument.\n");
				return -1;
			}
			break;
		case 'L':
			cmdargs.logfile = optarg;
			break;
		case 'f':
			cmdargs.force = 1;
			break;
		default:
			return -1;
		}
	}

	return 0;
}
bues.ch cgit interface