Debian Codegraph @

easel/esl_subcmd.c

raw symbols SCIP
1 /* esl_subcmd : utilities for command line programs that take subcommands
2 *
3 * Extends esl_getopts to more complicated programs with subcommands.
4 *
5 * See also:
6 * esl_getopts : command line argument parsing
7 */
8 #include <esl_config.h>
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "easel.h"
14 #include "esl_getopts.h"
15 #include "esl_subcmd.h"
16
17
18 /* Function: esl_subcmd_CreateDefaultApp()
19 * Synopsis: Process cmdline options for a subcommand
20 *
21 * Purpose: For a subcommand <sub> of main program <topcmd>, with
22 * subcommand options <suboptions>, process a command line
23 * <argc>/<argv>; return a new <ESL_GETOPTS> object on
24 * success.
25 *
26 * If there's a problem with the user's command line, print
27 * informative message and `exit(1)`. If the subcommand
28 * options include `-h`, print help and `exit(0)`.
29 *
30 * <sub> is one <ESL_SUBCMD> structure from the main
31 * application's table of subcommands. <topcmd> is a
32 * string, usually the same as <argv[0]>. <suboptions>
33 * is a table of <ESL_OPTIONS> for the subcommand.
34 *
35 * If <topcmd> is a path, e.g. "/foo/bar/easel", the
36 * leading path is removed, leaving just "easel" as the
37 * name of the main program.
38 *
39 * Args: topcmd - name of the main command, e.g. "easel" (= argv[0], probably)
40 * sub - ESL_SUBCMD table entry for this subcommand
41 * suboptions - ESL_OPTIONS table for this subcommand
42 * argc - number of args on the commandline
43 * argv - array of args on the commandline
44 *
45 * Returns: ptr to new <ESL_GETOPTS> object
46 *
47 * Throws: NULL on allocation failures.
48 */
49 ESL_GETOPTS *
50 esl_subcmd_CreateDefaultApp(const char *topcmd, const ESL_SUBCMD *sub, const ESL_OPTIONS *suboptions, int argc, char **argv)
51 {
52 ESL_GETOPTS *go = esl_getopts_Create(suboptions);
53 char *lastslash = strrchr(topcmd, '/');
54
55 if (lastslash) topcmd = lastslash+1;
56
57 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
58 esl_opt_VerifyConfig(go) != eslOK)
59 {
60 if ( esl_printf("Failed to parse command line: %s\n", go->errbuf) != eslOK) goto ERROR;
61 if ( esl_printf("Usage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR;
62 if ( esl_printf("\nTo see more help on available options, do `%s %s -h`\n\n", topcmd, sub->subcmd) != eslOK) goto ERROR;
63 exit(1);
64 }
65 if (esl_opt_GetBoolean(go, "-h") == TRUE)
66 {
67 if ( esl_printf("%s %s :: %s\n", topcmd, sub->subcmd, sub->description) != eslOK) goto ERROR;
68 if ( esl_printf("\nUsage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR;
69 if ( esl_printf("\nOptions:\n") != eslOK) goto ERROR;
70 if ( esl_opt_DisplayHelp(stdout, go, 0, 2, 80) != eslOK) goto ERROR;
71 exit(0);
72 }
73 if (esl_opt_ArgNumber(go) != sub->nargs)
74 {
75 if ( esl_printf("Incorrect number of command line arguments.") != eslOK) goto ERROR;
76 if ( esl_printf("Usage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR;
77 if ( esl_printf("\nTo see more help on available options, do `%s %s -h`\n\n", topcmd, sub->subcmd) != eslOK) goto ERROR;
78 exit(1);
79 }
80 return go;
81
82 ERROR:
83 esl_getopts_Destroy(go);
84 return NULL;
85 }