]> mj.ucw.cz Git - libucw.git/blob - ucw/getopt.h
Packages: Added a custom string to names of compiled libraries.
[libucw.git] / ucw / getopt.h
1 /*
2  *      UCW Library -- Parsing of configuration and command-line options
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2012 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_GETOPT_H
12 #define _UCW_GETOPT_H
13
14 #ifdef CONFIG_UCW_OWN_GETOPT
15 #include <ucw/getopt/getopt-sh.h>
16 #else
17 #include <getopt.h>
18 #endif
19
20 /***
21  * [[conf_getopt]]
22  * Loading by @cf_getopt()
23  * ~~~~~~~~~~~~~~~~~~~~~~~
24  ***/
25
26 /**
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.
29  */
30 extern char *cf_def_file;
31 /**
32  * Name of environment variable that can override what configuration is loaded.
33  * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
34  **/
35 extern char *cf_env_file;
36 /**
37  * Short options for loading configuration by @cf_getopt().
38  * Prepend to your own options.
39  **/
40 #define CF_SHORT_OPTS   "C:S:"
41 /**
42  * Long options for loading configuration by @cf_getopt().
43  * Prepend to your own options.
44  **/
45 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
46 /**
47  * Use this constant as @long_opts parameter of @cf_getopt() if you do
48  * not have any long options in your program.
49  **/
50 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
51 #ifndef CF_USAGE_TAB
52 #define CF_USAGE_TAB ""
53 #endif
54 /**
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.
58  **/
59 #define CF_USAGE        \
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
62
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"
66 #else
67 #define CF_LONG_OPTS_DEBUG
68 #define CF_USAGE_DEBUG
69 #endif
70
71 /**
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.
78  *
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.
82  **/
83 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
84
85 void reset_getopt(void);        /** If you want to start parsing of the arguments from the first one again. **/
86
87 #endif