~laumann/C

74c6c80ca891acc54bb96e3aac560ca2ce1635e4 — Thomas Bracht Laumann Jespersen 9 years ago 53ec2f3
Added help message and -h,--help option.
Signed-off-by: Thomas Bracht Laumann Jespersen <laumann.thomas@gmail.com>
1 files changed, 14 insertions(+), 3 deletions(-)

M progress/use_progress.c
M progress/use_progress.c => progress/use_progress.c +14 -3
@@ 5,6 5,14 @@
#define TOTAL	300
#define DELAY	200

static char usage_string[] =
	"Usage: use_progress [options]...\n\n"
	"Options:\n"
	"  -t, --total <int>     The total progress.\n"
	"  -d, --delay <int>     The delay to add.\n"
	"  -m, --message <msg>   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);