/* ************************************************************************** */ /* _____ _ */ /* args.c |_ _|__ _ _| |__ ___ _ _ */ /* | Project: mcsfile | |/ _ \| | | | '_ \ / _ \ | | | */ /* | | (_) | |_| | | | | __/ |_| | */ /* By: thomas |_|\___/ \__,_|_| |_|\___|\__, |.fr */ /* Last updated: 2016/12/18 01:20:51 |___/ */ /* */ /* ************************************************************************** */ #include "main.h" #include #define Q(x) #x #define QUOTE(x) Q(x) /* ************************************************************************** */ /* Help and version messages */ /* ************************************************************************** */ /* Help message */ static const char *help_message = "Usage: mcsfile [--version|-v] [--help|-h]\n" " \n" "\n" "Reads mcs files in a g1m file.\n" "\n" "Options are:\n" " -h, --help Display this help message.\n" " -v, --version Display the version message.\n" "\n" "Report bugs to " QUOTE(AUTHOR) "."; /* Version message */ static const char *version_message = "mcsfile - from g1mutils v" QUOTE(VERSION) " (licensed under " QUOTE(LICENSE) ")\n" "Made by " QUOTE(AUTHOR) ".\n" "\n" "This is free software; see the source for copying conditions.\n" "There is NO warranty; not even for MERCHANTABILITY or\n" "FITNESS FOR A PARTICULAR PURPOSE."; /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ /** * parse_args: * Args parsing main function. * * @arg ac the arguments count. * @arg av the arguments values. * @arg files pointer to the file paths tab to allocate. * @return if execution should stop. */ int parse_args(int ac, char **av, const char **path) { /* getopt elements */ const char *optstring = "hv"; const struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {} }; /* get options */ int c; opterr = 0; int help = 0, version = 0; while ((c = getopt_long(ac, av, optstring, longopts, NULL)) != -1) switch (c) { case 'h': help = 1; break; case 'v': version = 1; break; default: switch (optopt) { default: fprintf(stderr, "-%c: unknown option.\n", optopt); } return (1); } /* check parameters */ int pc = ac - optind; char **pv = &av[optind]; if (pc != 1) help = 1; else *path = *pv; /* display version or help message */ if (version) { puts(version_message); return (1); } else if (help) { puts(help_message); return (1); } /* no error */ return (0); }