summaryrefslogtreecommitdiff
path: root/ctdb/common/cmdline.h
blob: 1e11c66c76e66564b6bdd303923fbd3ce4c68d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
   Command line processing

   Copyright (C) Amitay Isaacs  2018

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __CTDB_CMDLINE_H__
#define __CTDB_CMDLINE_H__

#include <popt.h>
#include <talloc.h>

/**
 * @file cmdline.h
 *
 * @brief Command-line handling with options and commands
 *
 * This abstraction encapsulates the boiler-plate for parsing options,
 * commands and arguments on command-line.
 *
 * Options handling is done using popt.
 */

/**
 * @brief Abstract data structure holding command-line configuration
 */
struct cmdline_context;

/**
 * @brief A command definition structure
 *
 * @name is the name of the command
 * @fn is the implementation of the command
 * @msg_help is the help message describing command
 * @msg_args is the help message describing arguments
 *
 * A command name can be a single word or multiple words separated with spaces.
 *
 * An implementation function should return 0 on success and non-zero value
 * on failure.  This value is returned as result in @cmdline_run.
 */
struct cmdline_command {
	const char *name;
	int (*fn)(TALLOC_CTX *mem_ctx,
		  int argc,
		  const char **argv,
		  void *private_data);
	const char *msg_help;
	const char *msg_args;
};

/**
 * @brief convinience macro to define the end of commands list
 *
 * Here is an example of defining commands list.
 *
 * struct cmdline_command commands[] = {
 *	{ "command1", command1_func, "Run command1", NULL },
 *	{ "command2", command2_func, "Run command2", "<filename>" },
 *	CMDLINE_TABLEEND
 * };
 */
#define CMDLINE_TABLEEND  { NULL, NULL, NULL, NULL }

/**
 * @brief Initialize cmdline abstraction
 *
 * If there are no options, options can be NULL.
 *
 * Help options (--help, -h) are automatically added to the options.
 *
 * @param[in] mem_ctx Talloc memory context
 * @param[in] prog Program name
 * @param[in] options Command-line options
 * @param[in] commands Commands array
 * @param[out] result New cmdline context
 * @return 0 on success, errno on failure
 *
 * Freeing cmdline context will free up all the resources.
 */
int cmdline_init(TALLOC_CTX *mem_ctx,
		 const char *prog,
		 struct poptOption *options,
		 struct cmdline_command *commands,
		 struct cmdline_context **result);

/**
 * @brief Parse command line options and commands/arguments
 *
 * This function parses the arguments to process options and commands.
 *
 * This function should be passed the arguments to main() and parse_options
 * should be set to true.  If cmdline is used for handling second-level
 * commands, then parse_options should be set to false.
 *
 * If argv does not match any command, then ENOENT is returned.
 *
 * @param[in] cmdline Cmdline context
 * @param[in] argc Number of arguments
 * @param[in] argv Arguments array
 * @param[in] parse_options Whether to parse for options
 * @return 0 on success, errno on failure
 */
int cmdline_parse(struct cmdline_context *cmdline,
		  int argc,
		  const char **argv,
		  bool parse_options);

/**
 * @brief Excecute the function for the command matched by @cmdline_parse
 *
 * @param[in] cmdline Cmdline context
 * @param[in] private_data Private data for implementation function
 * @param[out] result Return value from the implementation function
 * @return 0 on success, errno on failure
 *
 * If help options are specified, then detailed help will be printed and
 * the return value will be EAGAIN.
 */
int cmdline_run(struct cmdline_context *cmdline,
		void *private_data,
		int *result);

/**
 * @brief Print usage help message to stdout
 *
 * @param[in] cmdline Cmdline context
 * @param[in] command Command string
 *
 * If command is NULL, then full help is printed.
 * If command is specified, then compact help is printed.
 */
void cmdline_usage(struct cmdline_context *cmdline, const char *command);

#endif /* __CTDB_CMDLINE_H__ */