Browse Source

Improvements to expseq help text

Getty Ritter 10 years ago
parent
commit
49aa717417
1 changed files with 21 additions and 11 deletions
  1. 21 11
      expseq/expseq.c

+ 21 - 11
expseq/expseq.c

@@ -2,34 +2,44 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#define HELPMSG "USAGE: expseq LAST\n  or:  expseq FIRST LAST\n  or:  expseq --help\n"\
+                "Print numbers 2^FIRST to 2^LAST in exponential steps.\n\n"\
+                "If FIRST is omitted, it defaults to 0.\n"
+
 int main(int argc, char* argv[])
 {
-  int fstnum = 0, lstnum;
+  int fstnum = 0, lstnum, i;
   if (argc == 2) {
-	char* endptr = argv[1];
-	lstnum = strtol(argv[1], &endptr, 10);
-	if (!endptr) {
-	  fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[1]);
-	  return 0;
-	}
+        if (strcmp(argv[1], "--help")==0
+            || strcmp(argv[1], "-h")==0) {
+          fprintf(stderr, HELPMSG);
+          return 1;
+        } else {
+	  char* endptr = argv[1];
+	  lstnum = strtol(argv[1], &endptr, 10);
+	  if (!endptr) {
+	    fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[1]);
+	    return 1;
+	  }
+        }
   } else if (argc == 3) {
 	char* endptr = argv[1];
 	fstnum = strtol(argv[1], &endptr, 10);
 	if (!endptr) {
 	  fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[1]);
-	  return 0;
+	  return 1;
 	}
 	endptr = argv[2];
 	lstnum = strtol(argv[2], &endptr, 10);
 	if (!endptr) {
 	  fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[2]);
-	  return 0;
+	  return 1;
 	}
   } else {
 	  fprintf(stderr, "%s: missing operand\n", argv[0]);
-	return 0;
+	return 1;
   }
-  int i = fstnum;
+  i = fstnum;
   while (i <= lstnum) {
 	printf("%d\n", (int) pow(2, (float) i));
 	i++;