Better usage info in add example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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;