Skip to content

Commit 106998f

Browse files
committed
examples: print available commands if no args are given
1 parent ead1078 commit 106998f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

examples/lg2.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ static int run_command(git_command_fn fn, git_repository *repo, struct args_info
5050
return !!error;
5151
}
5252

53+
static int usage(const char *prog)
54+
{
55+
size_t i;
56+
57+
fprintf(stderr, "usage: %s <cmd>...\n\nAvailable commands:\n\n", prog);
58+
for (i = 0; i < ARRAY_SIZE(commands); i++)
59+
fprintf(stderr, "\t%s\n", commands[i].name);
60+
61+
exit(EXIT_FAILURE);
62+
}
63+
5364
int main(int argc, char **argv)
5465
{
5566
struct args_info args = ARGS_INFO_INIT;
@@ -58,10 +69,8 @@ int main(int argc, char **argv)
5869
int return_code = 1;
5970
size_t i;
6071

61-
if (argc < 2) {
62-
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
63-
exit(EXIT_FAILURE);
64-
}
72+
if (argc < 2)
73+
usage(argv[0]);
6574

6675
git_libgit2_init();
6776

@@ -79,6 +88,9 @@ int main(int argc, char **argv)
7988
}
8089
}
8190

91+
if (args.pos == args.argc)
92+
usage(argv[0]);
93+
8294
if (!git_dir)
8395
git_dir = ".";
8496

0 commit comments

Comments
 (0)