2 Command line processing
4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __CTDB_CMDLINE_H__
21 #define __CTDB_CMDLINE_H__
29 * @brief Command-line handling with options and commands
31 * This abstraction encapsulates the boiler-plate for parsing options,
32 * commands and arguments on command-line.
34 * Options handling is done using popt.
38 * @brief Abstract data structure holding command-line configuration
40 struct cmdline_context
;
43 * @brief A command definition structure
45 * @name is the name of the command
46 * @fn is the implementation of the command
47 * @msg_help is the help message describing command
48 * @msg_args is the help message describing arguments
50 * A command name can be a single word or multiple words separated with spaces.
52 * An implementation function should return 0 on success and non-zero value
53 * on failure. This value is returned as result in @cmdline_run.
55 struct cmdline_command
{
57 int (*fn
)(TALLOC_CTX
*mem_ctx
,
66 * @brief convinience macro to define the end of commands list
68 * Here is an example of defining commands list.
70 * struct cmdline_command commands[] = {
71 * { "command1", command1_func, "Run command1", NULL },
72 * { "command2", command2_func, "Run command2", "<filename>" },
76 #define CMDLINE_TABLEEND { NULL, NULL, NULL, NULL }
79 * @brief Initialize cmdline abstraction
81 * If there are no options, options can be NULL.
83 * Help options (--help, -h) are automatically added to the options.
85 * @param[in] mem_ctx Talloc memory context
86 * @param[in] prog Program name
87 * @param[in] options Command-line options
88 * @param[in] commands Commands array
89 * @param[out] result New cmdline context
90 * @return 0 on success, errno on failure
92 * Freeing cmdline context will free up all the resources.
94 int cmdline_init(TALLOC_CTX
*mem_ctx
,
96 struct poptOption
*options
,
97 struct cmdline_command
*commands
,
98 struct cmdline_context
**result
);
101 * @brief Parse command line options and commands/arguments
103 * This function parses the arguments to process options and commands.
105 * This function should be passed the arguments to main() and parse_options
106 * should be set to true. If cmdline is used for handling second-level
107 * commands, then parse_options should be set to false.
109 * If argv does not match any command, then ENOENT is returned.
111 * @param[in] cmdline Cmdline context
112 * @param[in] argc Number of arguments
113 * @param[in] argv Arguments array
114 * @param[in] parse_options Whether to parse for options
115 * @return 0 on success, errno on failure
117 int cmdline_parse(struct cmdline_context
*cmdline
,
123 * @brief Excecute the function for the command matched by @cmdline_parse
125 * @param[in] cmdline Cmdline context
126 * @param[in] private_data Private data for implementation function
127 * @param[out] result Return value from the implementation function
128 * @return 0 on success, errno on failure
130 * If help options are specified, then detailed help will be printed and
131 * the return value will be EAGAIN.
133 int cmdline_run(struct cmdline_context
*cmdline
,
138 * @brief Print usage help message to stdout
140 * @param[in] cmdline Cmdline context
141 * @param[in] command Command string
143 * If command is NULL, then full help is printed.
144 * If command is specified, then compact help is printed.
146 void cmdline_usage(struct cmdline_context
*cmdline
, const char *command
);
148 #endif /* __CTDB_CMDLINE_H__ */