summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorEtienne Samson <samson.etienne@gmail.com>2019-11-06 11:08:35 +0100
committerEtienne Samson <samson.etienne@gmail.com>2019-11-06 11:12:34 +0100
commitc924f36a8bc4aa8e27cc3adabcb090f925d24be0 (patch)
treed00e1cc916efa89ae0c06cf1ffd539387cb37598 /examples
parent025a93577d9cff75ba36816d8957470aac03f1c7 (diff)
downloadlibgit2-c924f36a8bc4aa8e27cc3adabcb090f925d24be0.tar.gz
examples: keep track of whether we processed a "--" arg
Diffstat (limited to 'examples')
-rw-r--r--examples/args.c13
-rw-r--r--examples/args.h6
-rw-r--r--examples/checkout.c8
-rw-r--r--examples/lg2.c3
-rw-r--r--examples/log.c3
5 files changed, 23 insertions, 10 deletions
diff --git a/examples/args.c b/examples/args.c
index b228ae3dd..208c38256 100644
--- a/examples/args.c
+++ b/examples/args.c
@@ -168,3 +168,16 @@ int match_int_arg(
return 0;
return match_int_internal(out, found, allow_negative, opt);
}
+
+int match_arg_separator(struct args_info *args)
+{
+ if (args->opts_done)
+ return 1;
+
+ if (strcmp(args->argv[args->pos], "--") != 0)
+ return 0;
+
+ args->opts_done = 1;
+ args->pos++;
+ return 1;
+}
diff --git a/examples/args.h b/examples/args.h
index b08a534f3..2c68bdb8b 100644
--- a/examples/args.h
+++ b/examples/args.h
@@ -8,6 +8,7 @@ struct args_info {
int argc;
char **argv;
int pos;
+ int opts_done : 1; /**< Did we see a -- separator */
};
#define ARGS_INFO_INIT { argc, argv, 0, 0 }
#define ARGS_CURRENT(args) args->argv[args->pos]
@@ -76,4 +77,9 @@ extern int match_int_arg(
*/
extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
+/**
+ * Check if we're processing past the single -- separator
+ */
+extern int match_arg_separator(struct args_info *args);
+
#endif
diff --git a/examples/checkout.c b/examples/checkout.c
index b706e044e..70b51859c 100644
--- a/examples/checkout.c
+++ b/examples/checkout.c
@@ -65,7 +65,7 @@ static void parse_options(const char **repo_path, checkout_options *opts, struct
const char *curr = args->argv[args->pos];
int bool_arg;
- if (strcmp(curr, "--") == 0) {
+ if (match_arg_separator(args)) {
break;
} else if (!strcmp(curr, "--force")) {
opts->force = 1;
@@ -190,11 +190,7 @@ int lg2_checkout(git_repository *repo, int argc, char **argv)
goto cleanup;
}
- if (args.pos >= args.argc) {
- fprintf(stderr, "unhandled\n");
- err = -1;
- goto cleanup;
- } else if (!strcmp("--", args.argv[args.pos])) {
+ if (match_arg_separator(&args)) {
/**
* Try to checkout the given path
*/
diff --git a/examples/lg2.c b/examples/lg2.c
index f1a8843d2..a3987c34d 100644
--- a/examples/lg2.c
+++ b/examples/lg2.c
@@ -84,8 +84,7 @@ int main(int argc, char **argv)
break;
} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
continue;
- } else if (!strcmp(a, "--")) {
- /* arg separator */
+ } else if (match_arg_separator(&args)) {
break;
}
}
diff --git a/examples/log.c b/examples/log.c
index a6bd957ae..3eee7d421 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -424,8 +424,7 @@ static int parse_options(
else
/** Try failed revision parse as filename. */
break;
- } else if (!strcmp(a, "--")) {
- ++args.pos;
+ } else if (!match_arg_separator(&args)) {
break;
}
else if (!strcmp(a, "--date-order"))