]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/test/utils.c
opt: add new parse_early_args_incomplete.
[ccan] / ccan / opt / test / utils.c
index 21e0b727ebd6cab4762526dc5a21a11a3eb5f072..2ff04884ebdc0efe6dc4d9dd8fe4ae6af4b9175b 100644 (file)
@@ -3,13 +3,12 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <ccan/opt/opt.h>
 #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;
 #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;
 {
        test_cb_called++;
        return NULL;
@@ -77,12 +76,56 @@ bool parse_args(int *argc, char ***argv, ...)
 
        *argv = a;
        allocated = true;
 
        *argv = a;
        allocated = true;
-       /* Re-set before parsing. */
-       optind = 0;
 
        return opt_parse(argc, *argv, save_err_output);
 }
 
 
        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"),
 struct opt_table short_table[] = {
        /* Short opts, different args. */
        OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),