This patch teaches config a new function, git_config_option_string(), which parses a string using parse-options. This is useful for any command that allows command-line options to appear in a config. Signed-off-by: Jay Soffian <jaysoffian@xxxxxxxxx> --- Originally I had just factored this into its own function in builtin-merge, but it seemed that it might be generally useful, so I moved it to config.c cache.h | 2 ++ config.c | 39 +++++++++++++++++++++++++++++++++++++++ parse-options.c | 2 ++ 3 files changed, 43 insertions(+), 0 deletions(-) diff --git a/cache.h b/cache.h index 189151d..c6d3f05 100644 --- a/cache.h +++ b/cache.h @@ -854,6 +854,8 @@ extern unsigned long git_config_ulong(const char *, const char *); extern int git_config_bool_or_int(const char *, const char *, int *); extern int git_config_bool(const char *, const char *); extern int git_config_string(const char **, const char *, const char *); +struct option; +extern int git_config_option_string(const struct option *, int, const char *, const char *); extern int git_config_set(const char *, const char *); extern int git_config_set_multivar(const char *, const char *, const char *, int); extern int git_config_rename_section(const char *, const char *); diff --git a/config.c b/config.c index 0c8c76f..29cfd5b 100644 --- a/config.c +++ b/config.c @@ -7,6 +7,7 @@ */ #include "cache.h" #include "exec_cmd.h" +#include "parse-options.h" #define MAXNAME (256) @@ -353,6 +354,44 @@ int git_config_string(const char **dest, const char *var, const char *value) return 0; } +int git_config_option_string(const struct option *options, int flags, + const char *var, const char *value) +{ + int argc, ret; + const char **argv; + char *buf; + struct parse_opt_ctx_t ctx; + + if (!value) + return config_error_nonbool(var); + + buf = xstrdup(value); + if ((argc = split_cmdline(buf, &argv)) < 0) { + free(buf); + return error("Malformed value for %s", var); + } + argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); + memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); + argc++; + + parse_options_start(&ctx, argc, argv, flags); + switch (parse_options_step(&ctx, options, NULL)) { + case PARSE_OPT_DONE: + ret = parse_options_end(&ctx); + break; + case PARSE_OPT_HELP: /* not supported in a config */ + default: /* PARSE_OPT_UNKNOWN */ + if (ctx.argv[0][1] == '-') { + ret = error("unknown option `%s'", ctx.argv[0] + 2); + } else { + ret = error("unknown switch `%c'", *ctx.opt); + } + break; + } + free(buf); + return ret; +} + static int git_default_core_config(const char *var, const char *value) { /* This needs a better name */ diff --git a/parse-options.c b/parse-options.c index 4c5d09d..7996b50 100644 --- a/parse-options.c +++ b/parse-options.c @@ -356,6 +356,8 @@ int parse_options(int argc, const char **argv, const struct option *options, int usage_with_options_internal(const char * const *usagestr, const struct option *opts, int full) { + if (!usagestr) + return PARSE_OPT_HELP; fprintf(stderr, "usage: %s\n", *usagestr++); while (*usagestr && **usagestr) fprintf(stderr, " or: %s\n", *usagestr++); -- 1.6.2.rc2.332.g5d21b -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html