From 74c6c80ca891acc54bb96e3aac560ca2ce1635e4 Mon Sep 17 00:00:00 2001 From: Thomas Bracht Laumann Jespersen Date: Tue, 10 Jul 2012 20:26:32 +0200 Subject: [PATCH] Added help message and -h,--help option. Signed-off-by: Thomas Bracht Laumann Jespersen --- progress/use_progress.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/progress/use_progress.c b/progress/use_progress.c index ee95427..84e2fa1 100644 --- a/progress/use_progress.c +++ b/progress/use_progress.c @@ -5,6 +5,14 @@ #define TOTAL 300 #define DELAY 200 +static char usage_string[] = + "Usage: use_progress [options]...\n\n" + "Options:\n" + " -t, --total The total progress.\n" + " -d, --delay The delay to add.\n" + " -m, --message Set process message.\n" + " -h, --help Print this help message and exit.\n"; + /* Keep the configuration in a struct */ static struct { int delay; @@ -37,24 +45,27 @@ read_cmd_args(int nargs, char **argv) for (i=0 ; i < nargs ; i++) { arg = argv[i]; - if (!strcmp(arg, "--delay") || !strcmp(arg, "-d")) { + if (!strcmp(arg, "-d") || !strcmp(arg, "--delay")) { if (i+1 > nargs) { printf("Missing integer argument to %s.\n", arg); exit(EXIT_FAILURE); } parse_int_arg(arg, argv[++i], &config.delay); - } else if (!strcmp(arg, "--total") || !strcmp(arg, "-t")) { + } else if (!strcmp(arg, "-t") || !strcmp(arg, "--total")) { if (i+1 > nargs) { printf("Missing integer argument to %s.\n", arg); exit(EXIT_FAILURE); } parse_int_arg(arg, argv[++i], &config.total); - } else if (!strcmp(arg, "--message") || !strcmp(arg, "-m")) { + } else if (!strcmp(arg, "-m") || !strcmp(arg, "--message")) { if (i+1 > nargs) { printf("Missing string argument to %s.\n", arg); exit(EXIT_FAILURE); } config.msg = argv[++i]; /* No errors here */ + } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) { + printf("%s", usage_string); + exit(EXIT_SUCCESS); } else { if (arg[0] == '-') printf("Unrecognized option: %s\n", arg); -- 2.45.2