[PATCH] Have a flag to stop the option parsing at the first argument.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx>
---
    >     // ...
    >     /* when in `git --opt1 --opt2 foo -a -b -c` mode: */
    >     int cmd_pos = git_find_builtin_command_name(argc, argv);
    >     int count = parse_options(cmd_pos, argv, git_generic_options,
    > 	"git [special-options] cmd [options]", 0);
    >     if (count)
    > 	die("unknown git command: `%s`", argv[0]);
    >     argv += cmd_pos;
    >     argc -= cmd_pos;
    >     /* here we simulate an argv of {"foo", "-a", "-b", "-c"} */

    Or even simpler, with the following specifically tailored patch you
    can directly write:

    argc = parse_options(argc, argv, git_generic_options,
	"git [generic-options] <command> [cmd-options]",
	PARSE_OPT_STOP_AT_ARG);

    and then {argc, argv} will exactly be the NULL-terminated array
    starting with the builtin command. Kind of nice :)

 parse-options.c |    2 ++
 parse-options.h |    1 +
 2 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index 7a08a0c..4f5c55e 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -229,6 +229,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
 		const char *arg = args.argv[0];
 
 		if (*arg != '-' || !arg[1]) {
+			if (flags & PARSE_OPT_STOP_AT_ARG)
+				break;
 			argv[j++] = args.argv[0];
 			continue;
 		}
diff --git a/parse-options.h b/parse-options.h
index 102ac31..7c636b9 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -18,6 +18,7 @@ enum parse_opt_type {
 
 enum parse_opt_flags {
 	PARSE_OPT_KEEP_DASHDASH = 1,
+	PARSE_OPT_STOP_AT_ARG   = 2,
 };
 
 enum parse_opt_option_flags {
-- 
1.5.4.rc0.1147.gfd22d

Attachment: pgp3DMvVcIS3A.pgp
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux