+/* Licensed under GPLv3+ - see LICENSE file for details */
#ifndef CCAN_OPT_H
#define CCAN_OPT_H
#include <ccan/compiler/compiler.h>
{ (names), OPT_CB_NOARG((cb), (arg)), { (arg) }, (desc) }
/**
- * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
+ * OPT_WITH_ARG() - macro for initializing an opt_table entry (with arg)
* @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
* @cb: the callback when the option is found (along with <arg>).
* @show: the callback to print the value in get_usage (or NULL)
* where "type" is the type of the @arg argument. The first argument to the
* @cb is the argument found on the commandline.
*
- * At least one of @longopt and @shortopt must be non-zero. If the
- * @cb returns false, opt_parse() will stop parsing and return false.
+ * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
+ * returned string to form an error message for errlog(), free() the
+ * string and return false.
*
* Example:
* static char *explode(const char *optarg, void *unused)
* @errlog: the function to print errors
*
* This iterates through the command line and calls callbacks registered with
- * opt_register_table()/opt_register_arg()/opt_register_noarg(). If there
- * are unknown options, missing arguments or a callback returns false, then
- * an error message is printed and false is returned.
+ * opt_register_table()/opt_register_arg()/opt_register_noarg(). As this
+ * occurs successfully each option is removed from argc and argv.
*
- * On success, argc and argv are adjusted so only the non-option elements
- * remain, and true is returned.
+ * If there are unknown options, missing arguments or a callback
+ * returns false, then an error message is printed and false is
+ * returned: the erroneous option is not removed.
+ *
+ * On success, argc and argv will contain only the non-option
+ * elements, and true is returned.
*
* Example:
* if (!opt_parse(&argc, argv, opt_log_stderr)) {
bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
/**
- * opt_free_table - free the table.
+ * opt_free_table - reset the opt library.
*
- * This frees the internal memory. Call this as the last
- * opt function.
+ * This frees the internal memory and returns counters to zero. Call
+ * this as the last opt function to avoid memory leaks. You can also
+ * use this function to reset option handling to its initial state (no
+ * options registered).
*/
void opt_free_table(void);
char *opt_set_ulongval(const char *arg, unsigned long *ul);
void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
+/* the following setting functions accept k, M, G, T, P, or E suffixes, which
+ multiplies the numeric value by the corresponding power of 1000 or 1024
+ (for the _si and _bi versions, respectively).
+ */
+char *opt_set_intval_bi(const char *arg, int *i);
+char *opt_set_intval_si(const char *arg, int *i);
+char *opt_set_uintval_bi(const char *arg, unsigned int *u);
+char *opt_set_uintval_si(const char *arg, unsigned int *u);
+char *opt_set_longval_bi(const char *arg, long *l);
+char *opt_set_longval_si(const char *arg, long *l);
+char *opt_set_ulongval_bi(const char *arg, unsigned long *ul);
+char *opt_set_ulongval_si(const char *arg, unsigned long *ul);
+char *opt_set_longlongval_bi(const char *arg, long long *ll);
+char *opt_set_longlongval_si(const char *arg, long long *ll);
+char *opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll);
+char *opt_set_ulonglongval_si(const char *arg, unsigned long long *ll);
+
+
+void opt_show_intval_bi(char buf[OPT_SHOW_LEN], const int *x);
+void opt_show_longval_bi(char buf[OPT_SHOW_LEN], const long *x);
+void opt_show_longlongval_bi(char buf[OPT_SHOW_LEN], const long long *x);
+void opt_show_uintval_bi(char buf[OPT_SHOW_LEN], const unsigned int *x);
+void opt_show_ulongval_bi(char buf[OPT_SHOW_LEN], const unsigned long *x);
+void opt_show_ulonglongval_bi(char buf[OPT_SHOW_LEN], const unsigned long long *x);
+
+void opt_show_intval_si(char buf[OPT_SHOW_LEN], const int *x);
+void opt_show_longval_si(char buf[OPT_SHOW_LEN], const long *x);
+void opt_show_longlongval_si(char buf[OPT_SHOW_LEN], const long long *x);
+void opt_show_uintval_si(char buf[OPT_SHOW_LEN], const unsigned int *x);
+void opt_show_ulongval_si(char buf[OPT_SHOW_LEN], const unsigned long *x);
+void opt_show_ulonglongval_si(char buf[OPT_SHOW_LEN], const unsigned long long *x);
+
+
+
+
/* Increment. */
char *opt_inc_intval(int *i);