summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ff962a3)
raw | patch | inline | side by side (parent: ff962a3)
author | Pierre Habouzit <madcoder@debian.org> | |
Sun, 2 Mar 2008 10:35:56 +0000 (11:35 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 2 Mar 2008 22:07:47 +0000 (14:07 -0800) |
This is meant to be used to keep --not and --all during revision parsing.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
parse-options.c | patch | blob | history | |
parse-options.h | patch | blob | history | |
t/t0040-parse-options.sh | patch | blob | history | |
test-parse-options.c | patch | blob | history |
diff --git a/parse-options.c b/parse-options.c
index d9562ba5047ff1c9994755a4dbec5162b551c788..b32c9ea66c7ae1e83564cd25d63d5cfd6c1589b5 100644 (file)
--- a/parse-options.c
+++ b/parse-options.c
struct optparse_t {
const char **argv;
- int argc;
+ const char **out;
+ int argc, cpidx;
const char *opt;
};
continue;
rest = skip_prefix(arg, options->long_name);
+ if (options->type == OPTION_ARGUMENT) {
+ if (!rest)
+ continue;
+ if (*rest == '=')
+ return opterror(options, "takes no value", flags);
+ if (*rest)
+ continue;
+ p->out[p->cpidx++] = arg - 2;
+ return 0;
+ }
if (!rest) {
/* abbreviated? */
if (!strncmp(options->long_name, arg, arg_end - arg)) {
int parse_options(int argc, const char **argv, const struct option *options,
const char * const usagestr[], int flags)
{
- struct optparse_t args = { argv + 1, argc - 1, NULL };
- int j = 0;
+ struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
for (; args.argc; args.argc--, args.argv++) {
const char *arg = args.argv[0];
if (*arg != '-' || !arg[1]) {
- argv[j++] = args.argv[0];
+ args.out[args.cpidx++] = args.argv[0];
continue;
}
usage_with_options(usagestr, options);
}
- memmove(argv + j, args.argv, args.argc * sizeof(*argv));
- argv[j + args.argc] = NULL;
- return j + args.argc;
+ memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
+ args.out[args.cpidx + args.argc] = NULL;
+ return args.cpidx + args.argc;
}
#define USAGE_OPTS_WIDTH 24
pos += fprintf(stderr, "--%s", opts->long_name);
switch (opts->type) {
+ case OPTION_ARGUMENT:
+ break;
case OPTION_INTEGER:
if (opts->flags & PARSE_OPT_OPTARG)
pos += fprintf(stderr, " [<n>]");
diff --git a/parse-options.h b/parse-options.h
index 102ac31fb727acfdc3c2159e1525c7bcca94e1ef..dc0807834f9135b82c289b429a8d2ea13ee53be8 100644 (file)
--- a/parse-options.h
+++ b/parse-options.h
enum parse_opt_type {
/* special types */
OPTION_END,
+ OPTION_ARGUMENT,
OPTION_GROUP,
/* options with no arguments */
OPTION_BIT,
};
#define OPT_END() { OPTION_END }
+#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
index 0e2933a984fcf6a23b60e54ed43c411c699d4347..c23f0ace85c83f54fd7562d72af6ef6c1ea2eca3 100755 (executable)
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
--st <st> get another string (pervert ordering)
-o <str> get another string
+magic arguments
+ --quux means --quux
+
EOF
test_expect_success 'test help' '
git diff expect.err output.err
'
+cat > expect <<EOF
+boolean: 0
+integer: 0
+string: (not set)
+arg 00: --quux
+EOF
+
+test_expect_success 'keep some options as arguments' '
+ test-parse-options --quux > output 2> output.err &&
+ test ! -s output.err &&
+ git diff expect output
+'
+
test_done
diff --git a/test-parse-options.c b/test-parse-options.c
index eed8a02c6589bcb9af46188b56054eace3cd4d19..73360d75126af2f231b3cfdad26a47e18fff0a39 100644 (file)
--- a/test-parse-options.c
+++ b/test-parse-options.c
OPT_STRING(0, "string2", &string, "str", "get another string"),
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
OPT_STRING('o', NULL, &string, "str", "get another string"),
+ OPT_GROUP("magic arguments"),
+ OPT_ARGUMENT("quux", "means --quux"),
OPT_END(),
};
int i;