Skip to content

Commit c924f36

Browse files
committed
examples: keep track of whether we processed a "--" arg
1 parent 025a935 commit c924f36

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

examples/args.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,16 @@ int match_int_arg(
168168
return 0;
169169
return match_int_internal(out, found, allow_negative, opt);
170170
}
171+
172+
int match_arg_separator(struct args_info *args)
173+
{
174+
if (args->opts_done)
175+
return 1;
176+
177+
if (strcmp(args->argv[args->pos], "--") != 0)
178+
return 0;
179+
180+
args->opts_done = 1;
181+
args->pos++;
182+
return 1;
183+
}

examples/args.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct args_info {
88
int argc;
99
char **argv;
1010
int pos;
11+
int opts_done : 1; /**< Did we see a -- separator */
1112
};
1213
#define ARGS_INFO_INIT { argc, argv, 0, 0 }
1314
#define ARGS_CURRENT(args) args->argv[args->pos]
@@ -76,4 +77,9 @@ extern int match_int_arg(
7677
*/
7778
extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
7879

80+
/**
81+
* Check if we're processing past the single -- separator
82+
*/
83+
extern int match_arg_separator(struct args_info *args);
84+
7985
#endif

examples/checkout.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void parse_options(const char **repo_path, checkout_options *opts, struct
6565
const char *curr = args->argv[args->pos];
6666
int bool_arg;
6767

68-
if (strcmp(curr, "--") == 0) {
68+
if (match_arg_separator(args)) {
6969
break;
7070
} else if (!strcmp(curr, "--force")) {
7171
opts->force = 1;
@@ -190,11 +190,7 @@ int lg2_checkout(git_repository *repo, int argc, char **argv)
190190
goto cleanup;
191191
}
192192

193-
if (args.pos >= args.argc) {
194-
fprintf(stderr, "unhandled\n");
195-
err = -1;
196-
goto cleanup;
197-
} else if (!strcmp("--", args.argv[args.pos])) {
193+
if (match_arg_separator(&args)) {
198194
/**
199195
* Try to checkout the given path
200196
*/

examples/lg2.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ int main(int argc, char **argv)
8484
break;
8585
} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
8686
continue;
87-
} else if (!strcmp(a, "--")) {
88-
/* arg separator */
87+
} else if (match_arg_separator(&args)) {
8988
break;
9089
}
9190
}

examples/log.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ static int parse_options(
424424
else
425425
/** Try failed revision parse as filename. */
426426
break;
427-
} else if (!strcmp(a, "--")) {
428-
++args.pos;
427+
} else if (!match_arg_separator(&args)) {
429428
break;
430429
}
431430
else if (!strcmp(a, "--date-order"))

0 commit comments

Comments
 (0)