2 * UCW Library -- Parsing of command line options
4 * (c) 2013 Maria Matejka <mq@ucw.cz>
5 * (c) 2014 Martin Mares <mj@ucw.cz>
6 * (c) 2014 Pavel Charvat <pchar@ucw.cz>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
17 #include <ucw/xtypes.h>
22 #ifdef CONFIG_UCW_CLEAN_ABI
23 #define cf_def_file ucw_cf_def_file
24 #define cf_env_file ucw_cf_env_file
25 #define opt_conf_hook_internal ucw_opt_conf_hook_internal
26 #define opt_failure ucw_opt_failure
27 #define opt_handle_config ucw_opt_handle_config
28 #define opt_handle_dumpconfig ucw_opt_handle_dumpconfig
29 #define opt_handle_help ucw_opt_handle_help
30 #define opt_handle_set ucw_opt_handle_set
31 #define opt_help ucw_opt_help
32 #define opt_parse ucw_opt_parse
35 #define OPT_EXIT_BAD_ARGS 2
42 * Each option belongs to one of the following classes, which define
43 * the overall behavior of the option. In most cases, the classes
44 * are set automatically by <<macros,declaration macros>>.
46 * - `OPT_CL_END`: this is not a real option class, but a signal
47 * that the list of options ends.
48 * - `OPT_CL_BOOL`: a boolean option. If specified without an argument,
49 * it sets the corresponding variable to 1 (true). So does an argument of
50 * `1`, `y`, `yes`, or `true`. Conversely, an argument of `0`, `n`, `no`,
51 * or `false` sets the variable to 0 (false) and the same happens if
52 * the option is given as `--no-`'option' with no argument.
53 * - `OPT_CL_STATIC`: options of this class just take a value and store
55 * - `OPT_CL_MULTIPLE`: collect values from all occurrences of this
56 * option in a growing array (see `gary.h`).
57 * - `OPT_CL_SWITCH`: a multiple-choice switch, which sets the variable
58 * to a fixed value provided in option definition.
59 * - `OPT_CL_INC`: increments the variable (or decrements, if the
60 * `OPT_NEGATIVE` flag is set).
61 * - `OPT_CL_CALL`: instead of setting a variable, call a function
62 * and pass the value of the option to it.
63 * - `OPT_CL_SECTION`: not a real option, but an instruction to insert
64 * contents of another list of options.
65 * - `OPT_CL_HELP`: no option, just print a help text.
66 * - `OPT_CL_HOOK`: no option, but a definition of a <<hooks,hook>>.
67 * - `OPT_CL_BREAK`: when a given option occurs, stop parsing and keep
68 * the option in the argument list.
90 * The list of options is represented by `struct opt_section`, which points to
91 * a sequence of `struct opt_item`s.
93 * These structures are seldom used directly -- instead, they are produced
94 * by <<macros,declaration macros>>.
97 /** A section of option list. **/
99 const struct opt_item * opt;
102 /** A definition of a single option item. **/
104 const char * name; // long name (NULL if none)
105 int letter; // short name (0 if none)
106 void * ptr; // variable to store the value to
107 const char * help; // description in --help (NULL to omit the option from the help)
109 const struct opt_section * section; // subsection for OPT_CL_SECTION
110 int value; // value for OPT_CL_SWITCH
111 void (* call)(const struct opt_item * opt, const char * value, void * data); // function to call for OPT_CL_CALL
112 void (* hook)(const struct opt_item * opt, uint event, const char * value, void * data); // function to call for OPT_CL_HOOK
113 struct cf_user_type * utype; // specification of the user-defined type for CT_USER
114 const struct xtype * xtype; // specification of the extended type for CT_XTYPE
116 u16 flags; // as defined below (for hooks, event mask is stored instead)
117 byte cls; // enum opt_class
118 byte type; // enum cf_type
126 * Each option can specify a combination of the following flags.
129 #define OPT_REQUIRED 0x1 /** The option must be always present. **/
130 #define OPT_REQUIRED_VALUE 0x2 /** The option must have a value. **/
131 #define OPT_NO_VALUE 0x4 /** The option must have no value. **/
132 #define OPT_MAYBE_VALUE 0x8 /** The option may have a value. **/
133 #define OPT_NEGATIVE 0x10 /** Reversing the effect of OPT_INC or saving @false into OPT_BOOL. **/
134 #define OPT_LAST_ARG 0x40 /** Stop processing arguments after this line. **/
135 #define OPT_SINGLE 0x100 /** The option must appear at most once. **/
136 #define OPT_MULTIPLE 0x200 /** The option may appear multiple times; will save all the values into a simple list. **/
137 #define OPT_SEEN_AS_LONG 0x400 // Used internally to signal that we currently process the long form of the option
138 #define OPT_BEFORE_CONFIG 0x800 /** The option may appear before a config file is loaded. **/
139 #define OPT_HELP_COL 0x1000 /** Used for OPT_CL_HELP to signal that tabs switch columns. **/
140 #define OPT_INTERNAL 0x4000 // Used internally to ask for passing of struct opt_context to OPT_CALL
143 * If none of these flags are specified, a default is chosen automatically
144 * according to option class:
146 * - `OPT_MAYBE_VALUE` for `OPT_CL_STATIC`
147 * - `OPT_REQUIRED_VALUE` for `OPT_CL_MULTIPLE`
148 * - `OPT_NO_VALUE` for `OPT_CL_BOOL`, `OPT_CL_SWITCH` and `OPT_CL_INC`
149 * - An error is reported in all other cases.
151 #define OPT_VALUE_FLAGS (OPT_REQUIRED_VALUE | OPT_NO_VALUE | OPT_MAYBE_VALUE)
155 * Macros for declaration of options
156 * ---------------------------------
158 * In most cases, option definitions are built using these macros.
161 /** Used inside `struct opt_section` to start a list of items. **/
162 #define OPT_ITEMS .opt = ( struct opt_item[] )
164 /** No option, just a piece of help text. **/
165 #define OPT_HELP(line) { .help = line, .cls = OPT_CL_HELP }
167 /** Like OPT_HELP, but the help text uses tab characters to switch columns like help text for ordinary options does. **/
168 #define OPT_HELP_COLUMNS(line) { .help = line, .flags = OPT_HELP_COL, .cls = OPT_CL_HELP }
170 /** Standard `--help` option. **/
171 #define OPT_HELP_OPTION OPT_CALL(0, "help", opt_handle_help, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_NO_VALUE, "\tShow this help")
173 /** Boolean option. @target should be a variable of type `int`. **/
174 #define OPT_BOOL(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, int *), .help = desc, .flags = fl, .cls = OPT_CL_BOOL, .type = CT_INT }
176 /** String option. @target should be a variable of type `char *`. **/
177 #define OPT_STRING(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, char **), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_STRING }
179 /** Ordinary integer option. @target should be a variable of type `int`. **/
180 #define OPT_INT(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, int *), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_INT }
182 /** Unsigned integer option. @target should be a variable of type `uint`. **/
183 #define OPT_UINT(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, uint *), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_INT }
185 /** 64-bit integer option. @target should be a variable of type `u64`. **/
186 #define OPT_U64(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, u64 *), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_U64 }
188 /** Floating-point option. @target should be a variable of type `double`. **/
189 #define OPT_DOUBLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, double *), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_DOUBLE }
191 /** IP address option, currently IPv4 only. @target should be a variable of type `u32`. **/
192 #define OPT_IP(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, u32 *), .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_IP }
194 /** Multi-valued string option. @target should be a growing array of `char *`s. **/
195 #define OPT_STRING_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, char ***), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_STRING }
197 /** Multi-valued integer option. @target should be a growing array of `int`s. **/
198 #define OPT_INT_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, int **), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_INT }
200 /** Multi-valued unsigned integer option. @target should be a growing array of `uint`s. **/
201 #define OPT_UINT_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, uint **), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_INT }
203 /** Multi-valued 64-bit integer option. @target should be a growing array of `u64`s. **/
204 #define OPT_U64_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, u64 **), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_U64 }
206 /** Multi-valued floating-point option. @target should be a growing array of `double`s. **/
207 #define OPT_DOUBLE_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, double **), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_DOUBLE }
209 /** Multi-valued IPv4 address option. @target should be a growing array of `u32`s. **/
210 #define OPT_IP_MULTIPLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, u32 **), .help = desc, .flags = fl, .cls = OPT_CL_MULTIPLE, .type = CT_IP }
212 /** Switch option. @target should be a variable of type `int` and it will be set to the value @val. **/
213 #define OPT_SWITCH(shortopt, longopt, target, val, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, int *), .help = desc, .flags = fl, .cls = OPT_CL_SWITCH, .type = CT_LOOKUP, .u.value = val }
215 /** Incrementing option. @target should be a variable of type `int`. **/
216 #define OPT_INC(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = CHECK_PTR_TYPE(&target, int *), .flags = fl, .help = desc, .cls = OPT_CL_INC, .type = CT_INT }
218 /** Breakpoint option. When this option occurs, parsing is terminated and the option is kept in the argument array. **/
219 #define OPT_BREAK(shortopt, longopt, fl) { .letter = shortopt, .name = longopt, .flags = fl, .cls = OPT_CL_BREAK }
221 /* FIXME: Backwards compatibility only, should not be used anymore. */
222 #define OPT_UNS OPT_UINT
223 #define OPT_UNS_MULTIPLE OPT_UINT_MULTIPLE
227 * When this option appears, call the function @fn with parameters @item, @value, @data,
228 * where @item points to the <<struct_opt_item,`struct opt_item`>> of this option,
229 * @value contains the current argument of the option (NULL if there is none),
230 * and @data is specified here.
232 #define OPT_CALL(shortopt, longopt, fn, data, fl, desc) { .letter = shortopt, .name = longopt, .ptr = data, .help = desc, .u.call = fn, .flags = fl, .cls = OPT_CL_CALL, .type = CT_USER }
235 * An option with user-defined syntax. @ttype is a <<conf:struct_cf_user_type,`cf_user_type`>>
236 * describing the syntax, @target is a variable of the corresponding type. If the @OPT_REQUIRED_VALUE
237 * flag is not set, the parser must be able to parse a NULL value.
239 #define OPT_USER(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.utype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_STATIC, .type = CT_USER }
241 /** Multi-valued option of user-defined type. @target should be a growing array of the right kind of items. **/
242 #define OPT_USER_MULTIPLE(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.utype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_MULTIPLE, .type = CT_USER }
245 * An option with user-defined syntax. @xtype is a <<xtypes:struct_xtype,`xtype`>>
246 * describing the syntax, @target is a variable of the corresponding type. If the @OPT_REQUIRED_VALUE
247 * flag is not set, the parser must be able to parse a NULL value.
249 #define OPT_XTYPE(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.xtype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_STATIC, .type = CT_XTYPE }
251 /** Multi-valued option of extended type. @target should be a growing array of the right kind of items. **/
252 #define OPT_XTYPE_MULTIPLE(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.xtype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_MULTIPLE, .type = CT_XTYPE }
254 /** A sub-section. **/
255 #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec }
257 /** Declares a <<hooks,hook>> to call upon any event from the specified set. **/
258 #define OPT_HOOK(fn, data, events) { .cls = OPT_CL_HOOK, .u.hook = fn, .flags = events, .ptr = data }
260 /** A terminator signalling the end of the option list. **/
261 #define OPT_END { .cls = OPT_CL_END }
265 * Positional arguments
266 * --------------------
268 * In addition to short and long options, the parser can also process 'positional
269 * arguments', which don't start with a dash and whose meaning depends solely on
272 * Positional arguments are declared as options with short name `OPT_POSITIONAL(n)`
273 * (where `n` is the position of the argument, starting with 1) and long name
274 * NULL. To accept an arbitrary number of positional arguments, use
275 * `OPT_POSITIONAL_TAIL` instead, which matches all arguments, for which no
276 * `OPT_POSITIONAL` is defined. (In the latter case, you probably want to define
277 * the argument as `OPT_MULTIPLE` or `OPT_CALL`, so that the values do not
278 * overwrite each other.)
280 * Options and positional arguments can be mixed arbitrarily. When a `--` appears
281 * as an argument, it is understood as a signal that all other arguments are
284 * `OPT_REQUIRED` can be used with positional arguments, but all required arguments
285 * must come before the non-required ones. When `OPT_POSITIONAL_TAIL` is declared
286 * required, it means that it must match at least once.
288 * Ordering of positional arguments within the list of options need not match
289 * their positions. Holes in position numbering are inadvisable.
292 #define OPT_POSITIONAL(n) (OPT_POSITIONAL_TAIL+(n))
293 #define OPT_POSITIONAL_TAIL 128
302 * Parse all arguments, given in a NULL-terminated array of strings.
304 * Typically, this is called from `main(argc, argv)` as `opt_parse(options, argv+1)`,
305 * skipping the 0th argument, which contains program name.
307 * Returns the number of arguments used (which need not be all of them
308 * if `OPT_LAST_ARG` was encountered).
310 * The argument array is left untouched.
311 * However, option values are not necessarily copied, the variables
312 * set by the parser may point to the argument array.
314 int opt_parse(const struct opt_section * options, char ** argv);
317 * Report parsing failure, suggest `--help`, and abort the program with
320 void opt_failure(const char * mesg, ...) FORMAT_CHECK(printf,1,2) NONRET;
322 void opt_help(const struct opt_section * sec);
323 void opt_handle_help(const struct opt_item * opt, const char * value, void * data);
327 * Cooperating with config file parser
328 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
330 * Parsing of command-line options and configuration files are usually
331 * intertwined in a somewhat tricky way. We want to provide command-line
332 * options that control the name of the configuration file, or possibly to
333 * override configuration settings from the command line. On the other hand,
334 * regular command-line options can refer to values loaded from the
335 * program's configuration.
337 * To achieve this goal, the option parser is able to cooperate with the
338 * config file parser. This is enabled by listing the `OPT_CONF_OPTIONS`
339 * macro in the list of command-line options.
341 * The following options are defined for you:
343 * - `-C` (`--config`) to load a specific configuration file. This option
344 * suppresses loading of the default configuration, but it can be given
345 * multiple times to merge settings from several files.
347 * - `-S` (`--set`) to include a part of configuration inline. For example,
348 * you can use `-Ssection.item=value` to change a single configuration item.
350 * - `--dumpconfig` to dump the configuration to standard output and exit.
351 * (This is available only if the program is compiled with `CONFIG_UCW_DEBUG`.)
353 * The default configuration file (as specified by <<var_cf_def_file,`cf_def_file`>>) is loaded
354 * as soon as the first option different from `-C` is encountered, unless
355 * a different file has been already loaded. For this reason, `-C` must be
356 * the very first argument given to the program.
358 * This interface supersedes <<conf:getopt_h,`cf_getopt()`>>.
361 #ifdef CONFIG_UCW_DEBUG
362 #define OPT_CONF_OPTIONS OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_DUMPCONFIG, OPT_CONF_HOOK
364 #define OPT_CONF_OPTIONS OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_HOOK
367 #define OPT_CONF_CONFIG OPT_CALL('C', "config", opt_handle_config, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_REQUIRED_VALUE, "<file>\tOverride the default configuration file")
368 #define OPT_CONF_SET OPT_CALL('S', "set", opt_handle_set, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_REQUIRED_VALUE, "<item>\tManual setting of a configuration item")
369 #define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_handle_dumpconfig, NULL, OPT_INTERNAL | OPT_NO_VALUE, "\tDump program configuration")
370 #define OPT_CONF_HOOK OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE | OPT_HOOK_FINAL | OPT_HOOK_INTERNAL)
372 void opt_handle_config(const struct opt_item * opt, const char * value, void * data);
373 void opt_handle_set(const struct opt_item * opt, const char * value, void * data);
374 void opt_handle_dumpconfig(const struct opt_item * opt, const char * value, void * data);
375 void opt_conf_hook_internal(const struct opt_item * opt, uint event, const char * value, void * data);
377 // XXX: This is duplicated with <ucw/getopt.h>, but that one will hopefully go away one day.
379 * The name of the default configuration file (NULL if configuration has been
380 * already loaded). It is initialized to `CONFIG_UCW_DEFAULT_CONFIG`, but you
381 * usually want to replace it by your own config file.
383 extern char *cf_def_file;
385 * Name of environment variable that can override what configuration is loaded.
386 * Defaults to the value of the `CONFIG_UCW_ENV_VAR_CONFIG` compile-time option.
388 extern char *cf_env_file;
395 * You can supply hook functions, which will be called by the parser upon various
396 * events. Hooks are declared as option items of class `OPT_CL_HOOK`, whose @flags
397 * field specifies a mask of events the hook wants to receive.
399 * Please note that the hook interface is not considered stable yet,
400 * so it might change in future versions of libucw.
402 * The following events are defined:
405 #define OPT_HOOK_BEFORE_ARG 0x1 /** Call before option parsing **/
406 #define OPT_HOOK_BEFORE_VALUE 0x2 /** Call before value parsing **/
407 #define OPT_HOOK_AFTER_VALUE 0x4 /** Call after value parsing **/
408 #define OPT_HOOK_FINAL 0x8 /** Call just before @opt_parse() returns **/
409 #define OPT_HOOK_INTERNAL 0x4000 // Used internally to ask for passing of struct opt_context