Ver Fonte

Add usage to getframe

Martijn Braam há 1 ano atrás
pai
commit
a69fb9eb2a
1 ficheiros alterados com 28 adições e 3 exclusões
  1. 28 3
      util/getframe.c

+ 28 - 3
util/getframe.c

@@ -27,6 +27,18 @@ xioctl(int fd, int request, void *arg)
 	return r;
 }
 
+void
+usage(char *name)
+{
+	fprintf(stderr, "Usage: %s [-h] [-n offset] [-c camera] [-o file]\n", name);
+	fprintf(stderr, "Get a frame using libmegapixels\n\n");
+	fprintf(stderr, "Arguments:\n");
+	fprintf(stderr, "  -n offset   Get the Nth frame from the output (defaults to 5)\n");
+	fprintf(stderr, "  -c camera   Use a specific camera number\n");
+	fprintf(stderr, "  -o file     File to store the frame in\n");
+	fprintf(stderr, "  -h          Display this help text\n");
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -35,8 +47,9 @@ main(int argc, char *argv[])
 	long res;
 	char *end;
 	char *outfile = NULL;
+	int count = 5;
 
-	while ((c = getopt(argc, argv, "c:o:")) != -1) {
+	while ((c = getopt(argc, argv, "hc:n:o:")) != -1) {
 		switch (c) {
 			case 'c':
 				res = strtol(optarg, &end, 10);
@@ -49,6 +62,18 @@ main(int argc, char *argv[])
 			case 'o':
 				outfile = optarg;
 				break;
+			case 'n':
+				res = strtol(optarg, &end, 10);
+				if (end == optarg || end == NULL || *end != '\0') {
+					fprintf(stderr, "Invalid number for -n\n");
+					return 1;
+				}
+				count = (int) res;
+				break;
+			case 'h':
+				usage(argv[0]);
+				return 0;
+				break;
 			case '?':
 				if (optopt == 'd' || optopt == 'l') {
 					fprintf(stderr, "Option -%c requires an argument.\n", optopt);
@@ -59,6 +84,7 @@ main(int argc, char *argv[])
 				}
 				return 1;
 			default:
+				usage(argv[0]);
 				return 1;
 		}
 	}
@@ -157,7 +183,6 @@ main(int argc, char *argv[])
 	}
 
 
-	int count = 5;
 	while (count-- > 0) {
 		while (1) {
 			fd_set(fds);
@@ -185,7 +210,7 @@ main(int argc, char *argv[])
 
 			fprintf(stderr, "received frame\n");
 
-			if (count == 1 && outfile != NULL) {
+			if (count == 0 && outfile != NULL) {
 				FILE *fp = fopen(outfile, "w");
 				fwrite(buffers[buf.index].start, buf.bytesused, 1, fp);
 				fclose(fp);