add -h option (help)
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/got/got.1 b/got/got.1
index ccb72f2..1a35ccf 100644
--- a/got/got.1
+++ b/got/got.1
@@ -22,6 +22,7 @@
.Sh SYNOPSIS
.Nm
.Ar command
+.Op Fl h
.Op Ar arg ...
.Sh DESCRIPTION
The
@@ -31,6 +32,12 @@ utility is used to manage
repositories.
It prioritizes ease of use and simplicity over flexibility.
.Pp
+The options are as follows:
+.Bl -tag -width tenletters
+.It Fl h
+Display usage information.
+.El
+.Pp
The commands are as follows:
.Bl -tag -width Ds
.It Cm status
diff --git a/got/got.c b/got/got.c
index d426e6d..bc454f6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -37,6 +37,7 @@
struct cmd {
const char *cmd_name;
int (*cmd_main)(int, char *[]);
+ void (*cmd_usage)(void);
const char *cmd_descr;
};
@@ -47,9 +48,11 @@ int cmd_log(int, char *[]);
int cmd_status(int, char *[]);
struct cmd got_commands[] = {
- { "log", cmd_log, "show repository history" },
+ { "log", cmd_log, usage_log,
+ "show repository history" },
#ifdef notyet
- { "status", cmd_status, "show modification status of files" },
+ { "status", cmd_status, usage_status,
+ "show modification status of files" },
#endif
};
@@ -59,14 +62,18 @@ main(int argc, char *argv[])
struct cmd *cmd;
unsigned int i;
int ch;
+ int hflag = 0;
setlocale(LC_ALL, "");
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
- while ((ch = getopt(argc, argv, "")) != -1) {
+ while ((ch = getopt(argc, argv, "h")) != -1) {
switch (ch) {
+ case 'h':
+ hflag = 1;
+ break;
default:
usage();
/* NOTREACHED */
@@ -85,6 +92,9 @@ main(int argc, char *argv[])
if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
continue;
+ if (hflag)
+ got_commands[i].cmd_usage();
+
return got_commands[i].cmd_main(argc, argv);
/* NOTREACHED */
}
@@ -98,7 +108,7 @@ usage(void)
{
int i;
- fprintf(stderr, "usage: %s command [arg ...]\n\nAvailable commands:\n",
+ fprintf(stderr, "usage: %s [-h] command [arg ...]\n\nAvailable commands:\n",
getprogname());
for (i = 0; i < nitems(got_commands); i++) {
struct cmd *cmd = &got_commands[i];