2 * UCW Library -- Parsing of configuration and command-line options
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #ifdef CONFIG_UCW_OWN_GETOPT
15 #include <ucw/getopt/getopt-sh.h>
22 * Loading by @cf_getopt()
23 * ~~~~~~~~~~~~~~~~~~~~~~~
27 * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded.
28 * You can set it to something else manually.
30 extern char *cf_def_file;
32 * Name of environment variable that can override what configuration is loaded.
33 * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
35 extern char *cf_env_file;
37 * Short options for loading configuration by @cf_getopt().
38 * Prepend to your own options.
40 #define CF_SHORT_OPTS "C:S:"
42 * Long options for loading configuration by @cf_getopt().
43 * Prepend to your own options.
45 #define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG
47 * Use this constant as @long_opts parameter of @cf_getopt() if you do
48 * not have any long options in your program.
50 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
52 #define CF_USAGE_TAB ""
55 * This macro provides text describing usage of the configuration
56 * loading options. Concatenate with description of your options and
57 * write to the user, if he/she provides invalid options.
60 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
61 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
63 #ifdef CONFIG_UCW_DEBUG
64 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
65 #define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
67 #define CF_LONG_OPTS_DEBUG
68 #define CF_USAGE_DEBUG
72 * Takes care of parsing the command-line arguments, loading the
73 * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
74 * configuration options. The calling convention is the same as with GNU getopt_long(),
75 * but you must prefix your own short/long options by the
76 * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>> or
77 * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
79 * The default configuration file can be overwritten by the --config options,
80 * which must come first. During parsing of all other options, the configuration
81 * is already available.
83 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
85 void reset_getopt(void); /** If you want to start parsing of the arguments from the first one again. **/