#include <stdarg.h>
#include <stdlib.h>
#include <ccan/opt/opt.h>
-#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include "utils.h"
unsigned int test_cb_called;
-char *test_noarg(void *arg)
+char *test_noarg(void *arg UNNEEDED)
{
test_cb_called++;
return NULL;
*argv = a;
allocated = true;
- /* Re-set before parsing. */
- optind = 0;
return opt_parse(argc, *argv, save_err_output);
}
+bool parse_early_args(int *argc, char ***argv, ...)
+{
+ char **a;
+ va_list ap;
+
+ va_start(ap, argv);
+ *argc = 1;
+ a = malloc(sizeof(*a) * (*argc + 1));
+ a[0] = (*argv)[0];
+ while ((a[*argc] = va_arg(ap, char *)) != NULL) {
+ (*argc)++;
+ a = realloc(a, sizeof(*a) * (*argc + 1));
+ }
+
+ if (allocated)
+ free(*argv);
+
+ *argv = a;
+ allocated = true;
+
+ return opt_early_parse(*argc, *argv, save_err_output);
+}
+
+bool parse_early_args_incomplete(int *argc, char ***argv, ...)
+{
+ char **a;
+ va_list ap;
+
+ va_start(ap, argv);
+ *argc = 1;
+ a = malloc(sizeof(*a) * (*argc + 1));
+ a[0] = (*argv)[0];
+ while ((a[*argc] = va_arg(ap, char *)) != NULL) {
+ (*argc)++;
+ a = realloc(a, sizeof(*a) * (*argc + 1));
+ }
+
+ if (allocated)
+ free(*argv);
+
+ *argv = a;
+ allocated = true;
+
+ return opt_early_parse_incomplete(*argc, *argv, save_err_output);
+}
+
struct opt_table short_table[] = {
/* Short opts, different args. */
OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),