Add a CLI command to output docs on CLI commands to a file
authorRussell Bryant <russell@russellbryant.com>
Mon, 2 Jul 2007 20:42:14 +0000 (20:42 +0000)
committerRussell Bryant <russell@russellbryant.com>
Mon, 2 Jul 2007 20:42:14 +0000 (20:42 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@72937 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/cli.c

index 7607793..cefee0e 100644 (file)
@@ -972,6 +972,10 @@ static struct ast_cli_entry cli_module_unload_deprecated = NEW_CLI(handle_unload
 
 static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 
+#ifdef AST_DEVMODE
+static char *handle_core_dump_clidocs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+#endif
+
 static struct ast_cli_entry cli_cli[] = {
        /* Deprecated, but preferred command is now consolidated (and already has a deprecated command for it). */
        NEW_CLI(handle_nodebugchan_deprecated, "Disable debugging on channel(s)"),
@@ -1010,6 +1014,10 @@ static struct ast_cli_entry cli_cli[] = {
        { { "soft", "hangup", NULL },
        handle_softhangup, "Request a hangup on a given channel",
        softhangup_help, complete_ch_3 },
+
+#ifdef AST_DEVMODE
+       NEW_CLI(handle_core_dump_clidocs, "Dump CLI command documentation in LaTeX format"),
+#endif
 };
 
 /*!
@@ -1450,6 +1458,65 @@ static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *
        return RESULT_SUCCESS;
 }
 
+#ifdef AST_DEVMODE
+static char *handle_core_dump_clidocs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       struct ast_cli_entry *cli;
+       FILE *f;
+       const char *fn = "/tmp/ast_cli_commands.tex";
+       struct cli_iterator i = { NULL, NULL};
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "core dump clidocs";
+               e->usage =
+                       "Usage: core dump clidocs\n"
+                       "       Dump CLI command documentation to /tmp/ast_cli_commands.tex.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if (a->argc != e->args)
+               return CLI_SHOWUSAGE;
+
+       if (!(f = fopen(fn, "w+"))) {
+               ast_cli(a->fd, "Unable to open %s for writing!\n", fn);
+               return CLI_FAILURE;
+       }
+
+       fprintf(f, "%% This file is automatically generated by the \"core dump clidocs\" CLI command.  Any manual edits will be lost.\n");
+
+       AST_LIST_LOCK(&helpers);
+       while ((cli = cli_next(&i))) {
+               /* Hide commands that start with '_' */
+               if (cli->_full_cmd[0] == '_')
+                       continue;
+               /* Hide commands that are marked as deprecated. */
+               if (cli->deprecated)
+                       continue;
+
+               fprintf(f, "\\section{%s}\n"
+                               "\\subsection{Summary}\n"
+                               "\\begin{verbatim}\n"
+                               "%s\n"
+                               "\\end{verbatim}\n"
+                               "\\subsection{Usage}\n"
+                               "\\begin{verbatim}\n"
+                               "%s\n"
+                               "\\end{verbatim}\n\n\n", 
+                               cli->_full_cmd, cli->summary, cli->usage);
+       }
+       AST_LIST_UNLOCK(&helpers);
+
+       fclose(f);
+
+       ast_cli(a->fd, "Documentation has been dumped to %s\n", fn);
+
+       return CLI_SUCCESS;
+}
+#endif /* AST_DEVMODE */
+
 static char *parse_args(const char *s, int *argc, char *argv[], int max, int *trailingwhitespace)
 {
        char *dup, *cur;