]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt.h
Opt: Cleaned up opt-conf
[libucw.git] / ucw / opt.h
index 4fddf730ae2953ba119aeef5d940ca8ed599be62..f9332b8648c144cb72fde5936be538ac3b612076 100644 (file)
--- a/ucw/opt.h
+++ b/ucw/opt.h
@@ -2,6 +2,7 @@
  *     UCW Library -- Parsing of command line options
  *
  *     (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
+ *     (c) 2014 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 #include <stdlib.h>
 #include <stdio.h>
 
+#ifdef CONFIG_UCW_CLEAN_ABI
+#define cf_def_file ucw_cf_def_file
+#define cf_env_file ucw_cf_env_file
+#define opt_conf_hook_internal ucw_opt_conf_hook_internal
+#define opt_handle_config ucw_opt_handle_config
+#define opt_handle_dumpconfig ucw_opt_handle_dumpconfig
+#define opt_handle_help ucw_opt_handle_help
+#define opt_handle_set ucw_opt_handle_set
+#define opt_help ucw_opt_help
+#define opt_parse ucw_opt_parse
+#endif
+
 #define OPT_EXIT_BAD_ARGS 2
 
 /***
@@ -64,7 +77,6 @@ struct opt_section {
  *
  *  OPT_HELP_OPTION declares --help and prints a line about that
  *  OPT_HELP prints a line into help
- *  OPT_HELP2 prints two strings onto a line using the same tab structure as the option listing
  *  OPT_BOOL declares boolean option with an auto-negation (--sth and --no-sth). It's also possible to write --sth=y/yes/true/1/n/no/false/0.
  *  OPT_STRING, OPT_UNS, OPT_INT declare simple string/uns/int option
  *  OPT_SWITCH declares one choice of a switch statement; these have common target and different `value`s; last wins unless OPT_SINGLE is set;
@@ -81,7 +93,7 @@ struct opt_section {
  *
  ***/
 
-#define OPT_HELP_OPTION OPT_CALL(0, "help", opt_show_help_internal, NULL, OPT_NO_VALUE, "Show this help")
+#define OPT_HELP_OPTION(help) OPT_CALL(0, "help", opt_handle_help, &help, OPT_NO_VALUE, "\tShow this help")
 #define OPT_HELP(line) { .help = line, .cls = OPT_CL_HELP }
 #define OPT_BOOL(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_BOOL, .type = CT_INT }
 #define OPT_STRING(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_STRING }
@@ -115,16 +127,27 @@ struct opt_section {
 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_HOOK
 #endif
 
-#define OPT_CONF_CONFIG            OPT_CALL('C', "config", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Override the default configuration file")
-#define OPT_CONF_SET       OPT_CALL('S', "set", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Manual setting of a configuration item")
-#define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_conf_internal, NULL, OPT_NO_VALUE, "Dump program configuration")
+#define OPT_CONF_CONFIG            OPT_CALL('C', "config", opt_handle_config, NULL, OPT_BEFORE_CONFIG | OPT_REQUIRED_VALUE, "<file>\tOverride the default configuration file")
+#define OPT_CONF_SET       OPT_CALL('S', "set", opt_handle_set, NULL, OPT_BEFORE_CONFIG | OPT_REQUIRED_VALUE, "<item>\tManual setting of a configuration item")
+#define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_handle_dumpconfig, NULL, OPT_NO_VALUE, "\tDump program configuration")
 #define OPT_CONF_HOOK      OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE)
 
-void opt_conf_internal(struct opt_item * opt, const char * value, void * data);
+void opt_handle_config(struct opt_item * opt, const char * value, void * data);
+void opt_handle_set(struct opt_item * opt, const char * value, void * data);
+void opt_handle_dumpconfig(struct opt_item * opt, const char * value, void * data);
 void opt_conf_hook_internal(struct opt_item * opt, const char * value, void * data);
 
-extern int opt_parsed_count;       /** How many opts have been already parsed. **/
-extern int opt_conf_parsed_count;
+// XXX: This is duplicated with <ucw/getopt.h>, but that one will hopefully go away one day.
+/**
+ * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded.
+ * You can set it to something else manually.
+ */
+extern char *cf_def_file;
+/**
+ * Name of environment variable that can override what configuration is loaded.
+ * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
+ **/
+extern char *cf_env_file;
 
 /***
  * Predefined shortopt arguments
@@ -142,7 +165,7 @@ extern int opt_conf_parsed_count;
 // FIXME: Is numbering from 1 natural here?
 // FIXME: Are there any rules for mixing of positional arguments with options?
 #define OPT_POSITIONAL(n)   (OPT_POSITIONAL_TAIL+(n))
-#define OPT_POSITIONAL_TAIL 256
+#define OPT_POSITIONAL_TAIL 128
 
 
 /***
@@ -160,53 +183,33 @@ extern int opt_conf_parsed_count;
 #define OPT_LAST_ARG       0x40        /** Stop processing argv after this line **/
 #define OPT_SINGLE         0x100       /** Argument must appear at most once **/
 #define OPT_MULTIPLE       0x200       /** Argument may appear any time; will save all the values into a simple list **/
+#define OPT_SEEN_AS_LONG    0x400      // Used internally
+#define OPT_BEFORE_CONFIG   0x800      /** Argument may appear before config file is loaded **/
 #define OPT_HOOK_BEFORE_ARG    0x1000  /** Call before option parsing **/
 #define OPT_HOOK_BEFORE_VALUE  0x2000  /** Call before value parsing **/
 #define OPT_HOOK_AFTER_VALUE   0x4000  /** Call after value parsing **/
 
+void opt_help(const struct opt_section * sec);
+void opt_handle_help(struct opt_item * opt, const char * value, void * data);
 
-/***
- * Value flags defaults
- * ~~~~~~~~~~~~~~~~~~~~
- *
- * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
- * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
- * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
- * must be specified for OPT_CALL and OPT_USER.
- ***/
-
-static uns opt_default_value_flags[] = {
-  [OPT_CL_BOOL] = OPT_NO_VALUE,
-  [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
-  [OPT_CL_SWITCH] = OPT_NO_VALUE,
-  [OPT_CL_INC] = OPT_NO_VALUE,
-  [OPT_CL_CALL] = 0,
-  [OPT_CL_USER] = 0,
-  [OPT_CL_SECTION] = 0,
-  [OPT_CL_HELP] = 0
-};
-
-extern const struct opt_section * opt_section_root;
-void opt_help_internal(const struct opt_section * help);
-
-static void opt_help(void) {
-  opt_help_internal(opt_section_root);
-}
-
-static void opt_usage(void) {
+// FIXME: Should this be public?
+static inline void opt_usage(void) {
   fprintf(stderr, "Run with argument --help for more information.\n");
 }
 
-static void opt_show_help_internal(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
-  opt_help();
-  exit(0);
-}
-
 /**
- * Parse all the arguments.
+ * Parse all arguments, given in a NULL-terminated array of strings.
+ *
+ * Typically, this is called from `main(argc, argv)` as `opt_parse(options, argv+1)`,
+ * skipping the 0th argument, which contains program name.
+ *
+ * Returns the number of arguments used (which need not be all of them
+ * if `OPT_LAST_ARG` was encountered).
+ *
+ * The argument array is left untouched.
+ * However, option values are not necessarily copied, the variables
+ * set by the parser may point to the argument array.
  **/
-void opt_parse(const struct opt_section * options, char ** argv);
-// FIXME: When parsing finishes (possibly due to OPT_LAST_ARG), what is guaranteed
-// about the state of argv[]?
+int opt_parse(const struct opt_section * options, char ** argv);
 
 #endif