From dd78f75171336b2cdff3eaad3af4523b1999940e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 6 Nov 2016 13:21:11 +0100 Subject: Geml it self (just arguments parsing and some utils) --- src/args_parser.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/args_parser.c (limited to 'src/args_parser.c') diff --git a/src/args_parser.c b/src/args_parser.c new file mode 100644 index 0000000..be55e43 --- /dev/null +++ b/src/args_parser.c @@ -0,0 +1,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 +#include +#include +#include +#include "args_parser.h" +#include "utils.h" +#include "io.h" + +const char *help_text = + "Usage: geml [OPTIONS]... 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; +} -- cgit v1.2.3