From e9bd5bcdd092f9420f226a03ca66931f158283d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Thu, 7 Jul 2016 20:45:57 +0200 Subject: [PATCH] Now parsing commandline arguments. --- CMakeLists.txt | 2 +- src/Main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 080a54a..8ca72b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "") set(CMAKE_BUILD_TYPE RelWithDebInfo) endif() -set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wpedantic -Wmissing-declarations -Wfatal-errors -pedantic-errors") +set(CMAKE_C_FLAGS "-std=c11 -Wall -Wextra -Werror -Wpedantic -Wfatal-errors -Wmissing-declarations -pedantic-errors") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3") set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3") diff --git a/src/Main.c b/src/Main.c index d3f0cdb..b11a70c 100644 --- a/src/Main.c +++ b/src/Main.c @@ -1,14 +1,74 @@ #include +#include +#include +#include +#include +#include -// 0 = false; -// 1 = true; -char version[16] = "v0.5"; -int verbose = 0; +const char *version = "v0.5"; +bool helpFlag = false; +bool versionFlag = false; +bool verboseFlag = false; +char *infile = NULL; + +inline void printHelp(); + +int main(int argc, char **argv) { + int arg; + + while((arg = getopt(argc, argv, "hvVf:")) != -1) { + switch(arg) { + case 'h': + helpFlag = true; + break; + + case 'v': + verboseFlag = true; + break; + + case 'V': + versionFlag = true; + break; + + case 'f': + infile = optarg; + break; + + case '?': + if(optopt == 'f') { + fprintf(stderr, "Option -%c requires an argument.\n", optopt); + } else if(isprint(optopt)) { + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + } else { + fprintf(stderr, "Unknown character `\\x%x'.\n", optopt); + } + return 1; + + default: + abort(); + } + } + + if(helpFlag) { + printHelp(); + return 0; + } else if(versionFlag) { + printf("%s\n", version); + return 0; + } -int main(/*int argc, char **argv*/) { printf("MMMCalc %s, Copyright (c) 2016 Nicolás A. Ortega.\n", version); printf("This program comes with ABSOLUTELY NO WARRANTY.\n"); printf("This program is free software and you are welcome to redistribute\n"); printf("under the terms and conditions of the GNU GPLv3 or higher.\n\n"); return 0; } + +void printHelp() { + printf("Usage: mmmcalc [options]\n"); + printf("Options:\n"); + printf(" -f -- The file to read variables from.\n"); + printf(" -h -- Show this help information.\n"); + printf(" -v -- Be verbose, prints the equations used.\n"); + printf(" -V -- Print the version number.\n"); +}