/* ************************************************************************** */ /* _____ _ */ /* args.c |_ _|__ _ _| |__ ___ _ _ */ /* | Project: p7test | |/ _ \| | | | '_ \ / _ \ | | | */ /* | | (_) | |_| | | | | __/ |_| | */ /* By: thomas |_|\___/ \__,_|_| |_|\___|\__, |.fr */ /* Last updated: 2017/01/13 00:52:20 |___/ */ /* */ /* ************************************************************************** */ #include "main.h" #include #define Q(x) #x #define QUOTE(x) Q(x) /* ************************************************************************** */ /* Help and version messages */ /* ************************************************************************** */ /* Version message */ static const char version_message[] = QUOTE(BIN) " - from " QUOTE(NAME) " v" QUOTE(VERSION) " (licensed under GPLv2)\n" "Maintained by " QUOTE(MAINTAINER) ".\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 help message */ static const char help_main[] = "Usage: " QUOTE(BIN) " [--help] [--version]\n" "\n" "This utility is only there to test the libp7 server feature.\n" "It should not be used in production for anything else.\n" "\n" "General options:\n" " -h, --help Display the help page of the (sub)command and quit.\n" " -v, --version Display the version message and quit.\n" "\n" "Report bugs to " QUOTE(MAINTAINER) "."; /* ************************************************************************** */ /* Main args parsing function */ /* ************************************************************************** */ /** * parse_args: * Args parsing main function. * * @arg ac the arguments count. * @arg av the arguments values. * @return if execution should stop after this call. */ int parse_args(int ac, char **av) { /* define options */ const char shopts[] = "hv"; const struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0} }; /* get all options */ int c; opterr = 0; int help = 0, version = 0; while ((c = getopt_long(ac, av, shopts, longopts, NULL)) >= 0) switch (c) { case 'h': help = 1; break; case 'v': version = 1; break; case '?': break; } /* get parameters */ int pc = ac - optind; if (pc) help = 1; /* print help or version */ if (help) { puts(help_main); return (1); } else if (version) { puts(version_message); return (1); } /* no problem */ return (0); }