changeset 14:a01899a6e4bb

Extend help command to show usage information for commands
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Mon, 07 Aug 2017 18:16:47 +0200
parents cf81eb0c2d5a
children 3380c8fd9776
files cmd.c pwm.1.xml
diffstat 2 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/cmd.c	Mon Aug 07 16:59:47 2017 +0200
+++ b/cmd.c	Mon Aug 07 18:16:47 2017 +0200
@@ -169,7 +169,7 @@
     cmd_removegroup },
     { "ch", "changepassword", "changepassword", "Change password",
     cmd_changepassword },
-    { "h", "help", "help", "Show help text", cmd_help },
+    { "h", "help", "help [command]", "Show help text", cmd_help },
     { "w", "write", "write", "Write the database", cmd_write },
     { "q", "quit", "quit", "Quit", cmd_quit },
     { "Q", "Quit", "Quit", "Quit without checking", cmd_quit },
@@ -882,14 +882,24 @@
 {
 	struct cmd	*cmd;
 
-	if (argc != 1) {
+	if (argc > 2) {
 		return (CMD_USAGE);
 	}
 
-	printf("Commands:\n");
-	for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
-		printf("%-2s %-16s %s\n", cmd->abbrev_cmd, cmd->full_cmd,
-		    cmd->description);
+	if (argc == 2) {
+		for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
+			if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
+			    (strcmp(argv[1], cmd->full_cmd) == 0)) {
+				printf("%s\n", cmd->usage);
+				break;
+			}
+		}
+	} else {
+		printf("Commands:\n");
+		for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
+			printf("%-2s %-16s %s\n", cmd->abbrev_cmd,
+			    cmd->full_cmd, cmd->description);
+		}
 	}
 
 	return (CMD_OK);
--- a/pwm.1.xml	Mon Aug 07 16:59:47 2017 +0200
+++ b/pwm.1.xml	Mon Aug 07 18:16:47 2017 +0200
@@ -34,7 +34,7 @@
       <email>guido+pwm@berhoerster.name</email>
       <personblurb/>
     </author>
-    <date>6 August, 2017</date>
+    <date>7 August, 2017</date>
   </info>
   <refmeta>
     <refentrytitle>pwm</refentrytitle>
@@ -458,12 +458,19 @@
           <listitem>
             <cmdsynopsis>
               <command>help</command>
+              <arg choice="opt">
+                <replaceable>command</replaceable>
+              </arg>
             </cmdsynopsis>
             <cmdsynopsis>
               <command>h</command>
+              <arg choice="opt">
+                <replaceable>command</replaceable>
+              </arg>
               <sbr/>
             </cmdsynopsis>
-            <para>Display a list of all commands and a short help text.</para>
+            <para>Display a summary of all commands or usage information for
+            the specified <replaceable>command</replaceable>.</para>
           </listitem>
         </varlistentry>
         <varlistentry>