diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-02-08 17:56:45 +0000 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-02-08 17:56:45 +0000 |
commit | 21b0e4322a67e579cb43d52258df10014f3dc324 (patch) | |
tree | da9a0a01e1796cb33857f96b8c2333870fea99a8 | |
parent | 56d92f01a90c37fab54d3960ca97af80f70fbfd7 (diff) |
using --yes and --no with an argument, we can now replace the default
value printed on stdout respectively for a yes or a no.
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
-rw-r--r-- | cph.c | 57 |
1 files changed, 55 insertions, 2 deletions
@@ -21,13 +21,66 @@ #include <stdio.h> #include <stdlib.h> #include <time.h> +#include <getopt.h> static const char *answer_yes = "y"; static const char *answer_no = "n"; -int main(void) +static unsigned int seed_g; + +static struct option options_g[] = { + { + .name = "yes", + .has_arg = required_argument, + .flag = NULL, + .val = 'y', + }, { + .name = "no", + .has_arg = required_argument, + .flag = NULL, + .val = 'n', + }, { + .name = 0, + .has_arg = 0, + .flag = NULL, + .val = 0, + } +}; + +static int parse_arguments(int *argcp, char *argv[]) +{ + int opt; + int ret = 0; + + while ((opt = getopt_long(*argcp, argv, "y:n:s:", options_g, NULL)) >= 0) + { + switch (opt) { + case 'y': + answer_yes = optarg; + break; + case 'n': + answer_no = optarg; + break; + case '?': + ret = -1; + break; + default: + break; + } + } + + return ret; +} + +int main(int argc, char *argv[]) { - srand(time(NULL)); + seed_g = time(NULL); + if (parse_arguments(&argc, argv) < 0) { + fprintf(stderr, "usage: %s [OPTIONS]\n", argv[0]); + return -1; + } + + srand(seed_g); for (;;) { switch (rand() % 2) { |