Commit 1b6b95a858b5185349db73ced93c5f08850ba369

Stefan Sperling 2018-03-12T20:25:51

add -h option (help)

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];