#include "utilities.h" #include #define NOERROR 0 #define ARGERROR 1 void help(int exit_code) { puts("Usage:"); puts("surfaces-unies -i path [-o path] [-options]\n"); puts("The default action is to compress the mandatory input image to a .sf"); puts("file saved in the current directory."); puts("The input image MUST be saved in the ppm format."); puts("Options available:"); puts("-h --help\n\tdisplay the current message"); puts("-i --input\n\tpath to the input file (MANDATORY)"); puts("-o --output"); puts("\tpath to the output file (if the file already exists, it will be\n"); puts("-c --compress\n\tcompress the input file"); puts("-u --uncompress\n\tuncompresses the input file to the output file."); exit(exit_code); } struct Argres { char *input; char *output; bool compress; }; typedef struct Argres Argres; void get_args(Argres *args, int *c) { switch (*c) { case 0: break; case 'h': help(NOERROR); break; case 'i': (*args).input = optarg; break; case 'o': (*args).output = optarg; break; case 'c': (*args).compress = true; break; case 'u': (*args).compress = false; break; case '?': default: help(ARGERROR); } } Argres process_args(int t_argc, char *t_argv[]) { Argres res; res.input = NULL; res.output = NULL; while (true) { int option_index = 0; static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"input", required_argument, NULL, 'i'}, {"output", required_argument, NULL, 'o'}, {"compress", no_argument, NULL, 'c'}, {"uncompress", no_argument, NULL, 'u'}, {NULL, 0, NULL, 0}}; int c = getopt_long(t_argc, t_argv, "hi:o:cu", long_options, &option_index); if (c == -1) break; get_args(&res, &c); } return res; } int main(int argc, char **argv) { Argres argresults = process_args(argc, argv); if (argresults.input) { printf("input: %s\n", argresults.input); } else { fprintf(stderr, "ERROR: no input file."); help(ARGERROR); } if (!argresults.output) { argresults.output = "output.fs"; } printf("output: %s\n", argresults.output); printf("Compress? %s\n", (argresults.compress) ? "true" : "false"); return 0; }