Now parsing commandline arguments.

This commit is contained in:
Nicolás A. Ortega 2016-07-07 20:45:57 +02:00
parent 156f8de5c0
commit e9bd5bcdd0
No known key found for this signature in database
GPG Key ID: 4825F773B8D44EFF
2 changed files with 66 additions and 6 deletions

View File

@ -5,7 +5,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE RelWithDebInfo) set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif() 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_DEBUG "${CMAKE_C_FLAGS} -g -O0")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3") set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3")

View File

@ -1,14 +1,74 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <getopt.h>
#include <unistd.h>
// 0 = false; const char *version = "v0.5";
// 1 = true; bool helpFlag = false;
char version[16] = "v0.5"; bool versionFlag = false;
int verbose = 0; 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("MMMCalc %s, Copyright (c) 2016 Nicolás A. Ortega.\n", version);
printf("This program comes with ABSOLUTELY NO WARRANTY.\n"); printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
printf("This program is free software and you are welcome to redistribute\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"); printf("under the terms and conditions of the GNU GPLv3 or higher.\n\n");
return 0; return 0;
} }
void printHelp() {
printf("Usage: mmmcalc [options]\n");
printf("Options:\n");
printf(" -f <file> -- 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");
}