examples: print available commands if no args are given
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
diff --git a/examples/lg2.c b/examples/lg2.c
index 1f6fd85..b3df02d 100644
--- a/examples/lg2.c
+++ b/examples/lg2.c
@@ -50,6 +50,17 @@ static int run_command(git_command_fn fn, git_repository *repo, struct args_info
return !!error;
}
+static int usage(const char *prog)
+{
+ size_t i;
+
+ fprintf(stderr, "usage: %s <cmd>...\n\nAvailable commands:\n\n", prog);
+ for (i = 0; i < ARRAY_SIZE(commands); i++)
+ fprintf(stderr, "\t%s\n", commands[i].name);
+
+ exit(EXIT_FAILURE);
+}
+
int main(int argc, char **argv)
{
struct args_info args = ARGS_INFO_INIT;
@@ -58,10 +69,8 @@ int main(int argc, char **argv)
int return_code = 1;
size_t i;
- if (argc < 2) {
- fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
- exit(EXIT_FAILURE);
- }
+ if (argc < 2)
+ usage(argv[0]);
git_libgit2_init();
@@ -79,6 +88,9 @@ int main(int argc, char **argv)
}
}
+ if (args.pos == args.argc)
+ usage(argv[0]);
+
if (!git_dir)
git_dir = ".";