aboutsummaryrefslogtreecommitdiff
path: root/src/args_parser.c
blob: 0de5ca24cfce4e95053f167cb94aed0d3949b277 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* geml - General extendable macro language
 *   args_parser.c Program arguments parser
 *
 * Copyright (C) 2016 Karel Kočí
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include "args_parser.h"
#include "utils.h"
#include "io.h"

const char *help_text = 
	"Usage: geml [OPTION]... INPUT_FILE\n"
	"  General extendable macro language processing tool.\n"
	"\n"
	"Options:\n"
	"  -v, --verbose\n"
	"    Increases vebosity of output.\n"
	"  -q, --quiet\n"
	"    Dcreases verbosity of output.\n"
	"  -o, --output OUTPUT_FILE\n"
	"    Sets file in which output is going to be stored\n"
	"  -h, --help\n"
	"    Prints this text.\n"
	"  --version\n"
	"    Prints geml version\n"
	"\n"
	"Report bugs to: <" GEML_CONTACT ">\n"
	;

const char *version_text = "geml " GEML_VERSION "\n";

enum ARGS_ENUM {
	ARGS_ENUM_VERSION = 260
};

const struct option long_options[] = {
	{"verbose", no_argument, NULL, 'v'},
	{"quiet", no_argument, NULL, 'q'},
	{"output", required_argument, NULL, 'o'},
	{"help", no_argument, NULL, 'h'},
	{"version", no_argument, NULL, ARGS_ENUM_VERSION}
};

bool args_parse(int argc, char *argv[]) {
	char *output = NULL;

	int index = 1;
	int c;
	while((c = getopt_long(argc, argv, "vqo:h;", long_options, &index)) != -1) {
		switch (c) {
		case 'v': // verbose
			verbosity_inc();
			break;
		case 'q': // quiet
			verbosity_dec();
			break;
		case 'o': // output
			output = optarg;
			break;
		case 'h': // help
			fputs(help_text, stderr);
			return true;
		case ARGS_ENUM_VERSION:
			fputs(version_text, stderr);
			return true;
		default:
			abort();
		}
	}

	if (optind < argc) {
		if (!set_input(argv[optind]))
			abort();
		if ((argc - optind) > 1)
			WARN("Only first input file is accepted, rest is dropped.");
	}
	if (output != NULL && !set_output(output))
		abort();

	return false;
}