2 * UCW Library -- Parsing of command line options
4 * (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
19 #define OPT_EXIT_BAD_ARGS 2
26 OPT_CL_END, // end of list
27 OPT_CL_BOOL, // boolean value
28 OPT_CL_STATIC, // static value
29 OPT_CL_SWITCH, // lookup/switch
30 OPT_CL_INC, // incremental value
31 OPT_CL_CALL, // call a function
32 OPT_CL_USER, // user defined value
33 OPT_CL_SECTION, // subsection
34 OPT_CL_HELP, // help line
37 typedef void opt_custom_function(const char ** param);
41 const char * name; // long-op
42 void * ptr; // where to save
43 const char * help; // description in --help
45 struct opt_section * section; // subsection for OPT_SECTION
46 int value; // value for OPT_SWITCH
47 const char * help2; // second value for OPT_HELP2
48 int (* call)(const char ** param); // function to call for OPT_CALL
49 struct cf_user_type * utype; // specification of the user-defined type
51 const char letter; // short-op
53 byte cls; // enum opt_class
54 byte type; // enum cf_type
58 struct opt_item * opt;
61 #define OPT_ITEMS .opt = ( struct opt_item[] ) /** List of sub-items. **/
64 * Sub-items to be enclosed in OPT_ITEMS { } list
65 * ----------------------------------------------
67 * OPT_HELP_OPTION declares --help and prints a line about that
68 * OPT_HELP prints a line into help
69 * OPT_HELP2 prints two strings onto a line using the same tab structure as the option listing
70 * OPT_BOOL declares boolean option with an auto-negation (--sth and --no-sth); may be changed by OPT_BOOL_SET_PREFIXES
71 * OPT_STRING, OPT_UNS, OPT_INT declare simple string/uns/int option
72 * OPT_SWITCH declares one choice of a switch statement; these have common target and different `value`s; last wins unless OPT_SINGLE is set;
73 * parser fails if it matches an OPT_SWITCH with OPT_SINGLE set and also target set.
74 * Target must be of signed integer type; it is set to -1 if no switch appears at the command-line.
75 * OPT_CALL calls the given function with all the remaining command line, it returns the number of arguments to be skipped.
76 * OPT_USER declares a custom type of value; parser is of type opt_custom_parser
77 * and returns 1 on success and 0 on failure
78 * OPT_INC declares an incremental value like -v/--verbose
79 * OPT_SECTION declares a subsection
83 #define OPT_HELP_OPTION OPT_CALL(0, "help", opt_show_help_internal, OPT_NO_VALUE, "Show this help")
84 #define OPT_HELP(line) OPT_HELP2(line, NULL)
85 #define OPT_HELP2(first, second) { .help = first, .cls = OPT_CL_HELP, .u.help2 = second } // FIXME: remove this
86 #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 }
87 #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 }
88 #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 }
89 #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 }
90 #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 }
91 #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 }
92 #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 }
93 #define OPT_CALL(shortopt, longopt, fn, fl, desc) { .letter = shortopt, .name = longopt, .ptr = NULL, .help = desc, .u.call = fn, .flags = fl, .cls = OPT_CL_CALL, .type = CT_USER }
94 #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_USER, .type = CT_USER }
95 #define OPT_INC(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .flags = fl, .help = desc, .cls = OPT_CL_INC, .type = CT_INT }
96 #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec }
97 #define OPT_END { .cls = OPT_CL_END }
100 * Flags for the preceeding calls
101 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104 #define OPT_REQUIRED 0x1 /** Argument must appear at the command line **/
105 #define OPT_REQUIRED_VALUE 0x2 /** Argument must have a value **/
106 #define OPT_NO_VALUE 0x4 /** Argument must have no value **/
107 #define OPT_MAYBE_VALUE 0x8 /** Argument may have a value **/
108 #define OPT_VALUE_FLAGS (OPT_REQUIRED_VALUE | OPT_NO_VALUE | OPT_MAYBE_VALUE)
109 #define OPT_DECREMENT 0x10 /** Reversing the effect of OPT_INC **/
110 #define OPT_SINGLE 0x20 /** Argument must appear at most once **/
111 #define OPT_NO_HELP 0x40 /** Omit this line from help **/
114 * Value flags defaults
115 * ~~~~~~~~~~~~~~~~~~~~
117 * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
118 * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
119 * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
120 * must be specified for OPT_CALL and OPT_USER.
123 static uns opt_default_value_flags[] = {
124 [OPT_CL_BOOL] = OPT_NO_VALUE,
125 [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
126 [OPT_CL_SWITCH] = OPT_NO_VALUE,
127 [OPT_CL_INC] = OPT_NO_VALUE,
130 [OPT_CL_SECTION] = 0,
134 extern struct opt_section * opt_section_root;
135 void opt_help_noexit_internal(struct opt_section * help);
137 static void opt_help_noexit(void) {
138 opt_help_noexit_internal(opt_section_root);
141 static void opt_usage_noexit(void) {
142 fprintf(stderr, "Run with argument --help for more information.\n");
145 static int opt_show_help_internal(const char ** param UNUSED) {
150 static void opt_help(void) {
155 static void opt_usage(void) {
161 * Init the opt engine.
163 void opt_init(struct opt_section * options);
166 * Positional argument handler to be given to opt_parse()
168 typedef void opt_positional(const char * str);
171 * Parse all the arguments. Run the @callback for each of the positional argument.
173 void opt_parse(char ** argv, opt_positional * callback);