Commit 813937ce4e99f8ba208548ef557a776bbc7029f9

Krzysztof Adamski 2013-09-04T18:42:47

Better usage info in add example.

diff --git a/examples/add.c b/examples/add.c
index 12e660d..f2a33f9 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -58,6 +58,15 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
 	return ret;
 }
 
+void print_usage(void)
+{
+	fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
+	fprintf(stderr, "\t-n, --dry-run    dry run\n");
+	fprintf(stderr, "\t-v, --verbose    be verbose\n");
+	fprintf(stderr, "\t-u, --update     update tracked files\n");
+}
+
+
 int main (int argc, char** argv)
 {
 	git_index_matched_path_cb matched_cb = NULL;
@@ -67,11 +76,6 @@ int main (int argc, char** argv)
 	int i, options = 0;
 	struct print_payload payload = {0};
 
-	if (argc < 2) {
-		fprintf(stderr, "usage: add [-n|--dry-run] [-v|--verbose] [-u|--update] file-spec [file-spec] [...]\n");
-		return 1;
-	}
-
 	for (i = 1; i < argc; ++i) {
 		if (argv[i][0] != '-') {
 			break;
@@ -85,22 +89,31 @@ int main (int argc, char** argv)
 		else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
 			options |= UPDATE;
 		}
+		else if(!strcmp(argv[i], "-h")) {
+			print_usage();
+			break;
+		}
 		else if(!strcmp(argv[i], "--")) {
+			i++;
 			break;
 		}
 		else {
 			fprintf(stderr, "Unsupported option %s.\n", argv[i]);
+			print_usage();
 			return 1;
 		}
 	}
-	
-	init_array(&array, argc-i, argv+i);
 
 	printf("args:\n");
 	for(i=0; i<array.count; i++) {
 		printf(" - %s\n", array.strings[i]);
+	if (argc<=i) {
+		print_usage();
+		return 1;
 	}
 
+	init_array(&array, argc-i, argv+i);
+
 	if (git_repository_open(&repo, ".") < 0) {
 		fprintf(stderr, "No git repository\n");
 		return 1;