parse-opts: prepare for OPT_FILENAME

To give OPT_FILENAME the prefix, we pass the prefix to parse_options()
which passes the prefix to parse_options_start() which sets the prefix
member of parse_opts_ctx accordingly. If there isn't a prefix in the
calling context, passing NULL will suffice.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Stephen Boyd 2009-05-23 11:53:12 -07:00 committed by Junio C Hamano
parent 3d09e64ac1
commit 3778292017
51 changed files with 86 additions and 64 deletions

View File

@ -60,13 +60,13 @@ Steps to parse options
. in `cmd_foo(int argc, const char **argv, const char *prefix)` . in `cmd_foo(int argc, const char **argv, const char *prefix)`
call call
argc = parse_options(argc, argv, builtin_foo_options, builtin_foo_usage, flags); argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);
+ +
`parse_options()` will filter out the processed options of `argv[]` and leave the `parse_options()` will filter out the processed options of `argv[]` and leave the
non-option arguments in `argv[]`. non-option arguments in `argv[]`.
`argc` is updated appropriately because of the assignment. `argc` is updated appropriately because of the assignment.
+ +
You can also pass NULL instead of a usage array as fourth parameter of You can also pass NULL instead of a usage array as the fifth parameter of
parse_options(), to avoid displaying a help screen with usage info and parse_options(), to avoid displaying a help screen with usage info and
option list. This should only be done if necessary, e.g. to implement option list. This should only be done if necessary, e.g. to implement
a limited parser for only a subset of the options that needs to be run a limited parser for only a subset of the options that needs to be run

View File

@ -309,7 +309,7 @@ static int parse_archive_args(int argc, const char **argv,
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, opts, archive_usage, 0); argc = parse_options(argc, argv, NULL, opts, archive_usage, 0);
if (remote) if (remote)
die("Unexpected option --remote"); die("Unexpected option --remote");

View File

@ -298,7 +298,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
int add_new_files; int add_new_files;
int require_pathspec; int require_pathspec;
argc = parse_options(argc, argv, builtin_add_options, argc = parse_options(argc, argv, prefix, builtin_add_options,
builtin_add_usage, PARSE_OPT_KEEP_ARGV0); builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
if (patch_interactive) if (patch_interactive)
add_interactive = 1; add_interactive = 1;

View File

@ -3313,7 +3313,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
if (apply_default_whitespace) if (apply_default_whitespace)
parse_whitespace_option(apply_default_whitespace); parse_whitespace_option(apply_default_whitespace);
argc = parse_options(argc, argv, builtin_apply_options, argc = parse_options(argc, argv, prefix, builtin_apply_options,
apply_usage, 0); apply_usage, 0);
fake_ancestor = parse_options_fix_filename(prefix, fake_ancestor); fake_ancestor = parse_options_fix_filename(prefix, fake_ancestor);
if (fake_ancestor) if (fake_ancestor)

View File

@ -80,7 +80,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, local_opts, NULL, PARSE_OPT_KEEP_ALL); argc = parse_options(argc, argv, prefix, local_opts, NULL,
PARSE_OPT_KEEP_ALL);
if (output) if (output)
create_output_file(output); create_output_file(output);

View File

@ -17,7 +17,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, git_bisect_helper_usage, 0); argc = parse_options(argc, argv, prefix, options,
git_bisect_helper_usage, 0);
if (!next_all) if (!next_all)
usage_with_options(git_bisect_helper_usage, options); usage_with_options(git_bisect_helper_usage, options);

View File

@ -2229,7 +2229,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
save_commit_buffer = 0; save_commit_buffer = 0;
dashdash_pos = 0; dashdash_pos = 0;
parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH | parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
PARSE_OPT_KEEP_ARGV0); PARSE_OPT_KEEP_ARGV0);
for (;;) { for (;;) {
switch (parse_options_step(&ctx, options, blame_opt_usage)) { switch (parse_options_step(&ctx, options, blame_opt_usage)) {

View File

@ -610,7 +610,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
} }
hashcpy(merge_filter_ref, head_sha1); hashcpy(merge_filter_ref, head_sha1);
argc = parse_options(argc, argv, options, builtin_branch_usage, 0); argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
0);
if (!!delete + !!rename + !!force_create > 1) if (!!delete + !!rename + !!force_create > 1)
usage_with_options(builtin_branch_usage, options); usage_with_options(builtin_branch_usage, options);

View File

@ -231,7 +231,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
if (argc != 3 && argc != 2) if (argc != 3 && argc != 2)
usage_with_options(cat_file_usage, options); usage_with_options(cat_file_usage, options);
argc = parse_options(argc, argv, options, cat_file_usage, 0); argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
if (opt) { if (opt) {
if (argc == 1) if (argc == 1)

View File

@ -69,8 +69,8 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
int cnt, i, doubledash; int cnt, i, doubledash;
const char *errstr = NULL; const char *errstr = NULL;
argc = parse_options(argc, argv, check_attr_options, check_attr_usage, argc = parse_options(argc, argv, prefix, check_attr_options,
PARSE_OPT_KEEP_DASHDASH); check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
if (!argc) if (!argc)
usage_with_options(check_attr_usage, check_attr_options); usage_with_options(check_attr_usage, check_attr_options);

View File

@ -249,7 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
die("invalid cache"); die("invalid cache");
} }
argc = parse_options(argc, argv, builtin_checkout_index_options, argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
builtin_checkout_index_usage, 0); builtin_checkout_index_usage, 0);
state.force = force; state.force = force;
state.quiet = quiet; state.quiet = quiet;

View File

@ -605,7 +605,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
opts.track = BRANCH_TRACK_UNSPECIFIED; opts.track = BRANCH_TRACK_UNSPECIFIED;
argc = parse_options(argc, argv, options, checkout_usage, argc = parse_options(argc, argv, prefix, options, checkout_usage,
PARSE_OPT_KEEP_DASHDASH); PARSE_OPT_KEEP_DASHDASH);
/* --track without -b should DWIM */ /* --track without -b should DWIM */

View File

@ -56,7 +56,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
else else
config_set = 1; config_set = 1;
argc = parse_options(argc, argv, options, builtin_clean_usage, 0); argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
0);
memset(&dir, 0, sizeof(dir)); memset(&dir, 0, sizeof(dir));
if (ignored_only) if (ignored_only)

View File

@ -360,7 +360,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
junk_pid = getpid(); junk_pid = getpid();
argc = parse_options(argc, argv, builtin_clone_options, argc = parse_options(argc, argv, prefix, builtin_clone_options,
builtin_clone_usage, 0); builtin_clone_usage, 0);
if (argc == 0) if (argc == 0)

View File

@ -697,7 +697,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
{ {
int f = 0; int f = 0;
argc = parse_options(argc, argv, builtin_commit_options, usage, 0); argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
0);
logfile = parse_options_fix_filename(prefix, logfile); logfile = parse_options_fix_filename(prefix, logfile);
if (logfile) if (logfile)
logfile = xstrdup(logfile); logfile = xstrdup(logfile);

View File

@ -316,7 +316,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
config_exclusive_filename = getenv(CONFIG_ENVIRONMENT); config_exclusive_filename = getenv(CONFIG_ENVIRONMENT);
argc = parse_options(argc, argv, builtin_config_options, builtin_config_usage, argc = parse_options(argc, argv, prefix, builtin_config_options,
builtin_config_usage,
PARSE_OPT_STOP_AT_NON_OPTION); PARSE_OPT_STOP_AT_NON_OPTION);
if (use_global_config + use_system_config + !!given_config_file > 1) { if (use_global_config + use_system_config + !!given_config_file > 1) {

View File

@ -83,7 +83,7 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
OPT_END(), OPT_END(),
}; };
argc = parse_options(argc, argv, opts, count_objects_usage, 0); argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
/* we do not take arguments other than flags for now */ /* we do not take arguments other than flags for now */
if (argc) if (argc)
usage_with_options(count_objects_usage, opts); usage_with_options(count_objects_usage, opts);

View File

@ -322,7 +322,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
OPT_END(), OPT_END(),
}; };
argc = parse_options(argc, argv, options, describe_usage, 0); argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
if (max_candidates < 0) if (max_candidates < 0)
max_candidates = 0; max_candidates = 0;
else if (max_candidates > MAX_TAGS) else if (max_candidates > MAX_TAGS)

View File

@ -515,7 +515,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
init_revisions(&revs, prefix); init_revisions(&revs, prefix);
argc = setup_revisions(argc, argv, &revs, NULL); argc = setup_revisions(argc, argv, &revs, NULL);
argc = parse_options(argc, argv, options, fast_export_usage, 0); argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
if (argc > 1) if (argc > 1)
usage_with_options (fast_export_usage, options); usage_with_options (fast_export_usage, options);

View File

@ -634,7 +634,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
strbuf_addf(&default_rla, " %s", argv[i]); strbuf_addf(&default_rla, " %s", argv[i]);
argc = parse_options(argc, argv, argc = parse_options(argc, argv, prefix,
builtin_fetch_options, builtin_fetch_usage, 0); builtin_fetch_options, builtin_fetch_usage, 0);
if (argc == 0) if (argc == 0)

View File

@ -360,7 +360,8 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
int ret; int ret;
git_config(fmt_merge_msg_config, NULL); git_config(fmt_merge_msg_config, NULL);
argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0); argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
0);
if (argc > 0) if (argc > 0)
usage_with_options(fmt_merge_msg_usage, options); usage_with_options(fmt_merge_msg_usage, options);
inpath = parse_options_fix_filename(prefix, inpath); inpath = parse_options_fix_filename(prefix, inpath);

View File

@ -905,7 +905,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
OPT_END(), OPT_END(),
}; };
parse_options(argc, argv, opts, for_each_ref_usage, 0); parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
if (maxcount < 0) { if (maxcount < 0) {
error("invalid --count argument: `%d'", maxcount); error("invalid --count argument: `%d'", maxcount);
usage_with_options(for_each_ref_usage, opts); usage_with_options(for_each_ref_usage, opts);

View File

@ -590,7 +590,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
errors_found = 0; errors_found = 0;
argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0); argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
if (write_lost_and_found) { if (write_lost_and_found) {
check_full = 1; check_full = 1;
include_reflogs = 0; include_reflogs = 0;

View File

@ -194,7 +194,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
if (pack_refs < 0) if (pack_refs < 0)
pack_refs = !is_bare_repository(); pack_refs = !is_bare_repository();
argc = parse_options(argc, argv, builtin_gc_options, builtin_gc_usage, 0); argc = parse_options(argc, argv, prefix, builtin_gc_options,
builtin_gc_usage, 0);
if (argc > 0) if (argc > 0)
usage_with_options(builtin_gc_usage, builtin_gc_options); usage_with_options(builtin_gc_usage, builtin_gc_options);

View File

@ -765,7 +765,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
* unrecognized non option is the beginning of the refs list * unrecognized non option is the beginning of the refs list
* that continues up to the -- (if exists), and then paths. * that continues up to the -- (if exists), and then paths.
*/ */
argc = parse_options(argc, argv, options, grep_usage, argc = parse_options(argc, argv, prefix, options, grep_usage,
PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_DASHDASH |
PARSE_OPT_STOP_AT_NON_OPTION | PARSE_OPT_STOP_AT_NON_OPTION |
PARSE_OPT_NO_INTERNAL_HELP); PARSE_OPT_NO_INTERNAL_HELP);

View File

@ -423,7 +423,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
setup_git_directory_gently(&nongit); setup_git_directory_gently(&nongit);
git_config(git_help_config, NULL); git_config(git_help_config, NULL);
argc = parse_options(argc, argv, builtin_help_options, argc = parse_options(argc, argv, prefix, builtin_help_options,
builtin_help_usage, 0); builtin_help_usage, 0);
if (show_all) { if (show_all) {

View File

@ -936,7 +936,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
* like "git format-patch -o a123 HEAD^.." may fail; a123 is * like "git format-patch -o a123 HEAD^.." may fail; a123 is
* possibly a valid SHA1. * possibly a valid SHA1.
*/ */
argc = parse_options(argc, argv, builtin_format_patch_options, argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
builtin_format_patch_usage, builtin_format_patch_usage,
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN); PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);

View File

@ -486,7 +486,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
prefix_offset = strlen(prefix); prefix_offset = strlen(prefix);
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, builtin_ls_files_options, argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
ls_files_usage, 0); ls_files_usage, 0);
if (show_tag || show_valid_bit) { if (show_tag || show_valid_bit) {
tag_cached = "H "; tag_cached = "H ";

View File

@ -53,7 +53,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
}; };
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, options, merge_base_usage, 0); argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
if (argc < 2) if (argc < 2)
usage_with_options(merge_base_usage, options); usage_with_options(merge_base_usage, options);
rev = xmalloc(argc * sizeof(*rev)); rev = xmalloc(argc * sizeof(*rev));

View File

@ -48,7 +48,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
merge_style = git_xmerge_style; merge_style = git_xmerge_style;
} }
argc = parse_options(argc, argv, options, merge_file_usage, 0); argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
if (argc != 3) if (argc != 3)
usage_with_options(merge_file_usage, options); usage_with_options(merge_file_usage, options);
if (quiet) { if (quiet) {

View File

@ -462,7 +462,7 @@ static int git_merge_config(const char *k, const char *v, void *cb)
argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
argc++; argc++;
parse_options(argc, argv, builtin_merge_options, parse_options(argc, argv, NULL, builtin_merge_options,
builtin_merge_usage, 0); builtin_merge_usage, 0);
free(buf); free(buf);
} }
@ -855,7 +855,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
if (diff_use_color_default == -1) if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default; diff_use_color_default = git_use_color_default;
argc = parse_options(argc, argv, builtin_merge_options, argc = parse_options(argc, argv, prefix, builtin_merge_options,
builtin_merge_usage, 0); builtin_merge_usage, 0);
if (verbosity < 0) if (verbosity < 0)
show_diffstat = 0; show_diffstat = 0;

View File

@ -155,7 +155,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
OPT_END() OPT_END()
}; };
ac = parse_options(ac, av, option, mktree_usage, 0); ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
while (!got_eof) { while (!got_eof) {
while (1) { while (1) {

View File

@ -72,7 +72,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
if (read_cache() < 0) if (read_cache() < 0)
die("index file corrupt"); die("index file corrupt");
argc = parse_options(argc, argv, builtin_mv_options, builtin_mv_usage, 0); argc = parse_options(argc, argv, prefix, builtin_mv_options,
builtin_mv_usage, 0);
if (--argc < 1) if (--argc < 1)
usage_with_options(builtin_mv_usage, builtin_mv_options); usage_with_options(builtin_mv_usage, builtin_mv_options);

View File

@ -238,7 +238,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
}; };
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, opts, name_rev_usage, 0); argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
if (!!all + !!transform_stdin + !!argc > 1) { if (!!all + !!transform_stdin + !!argc > 1) {
error("Specify either a list, or --all, not both!"); error("Specify either a list, or --all, not both!");
usage_with_options(name_rev_usage, opts); usage_with_options(name_rev_usage, opts);

View File

@ -15,7 +15,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
OPT_BIT(0, "prune", &flags, "prune loose refs (default)", PACK_REFS_PRUNE), OPT_BIT(0, "prune", &flags, "prune loose refs (default)", PACK_REFS_PRUNE),
OPT_END(), OPT_END(),
}; };
if (parse_options(argc, argv, opts, pack_refs_usage, 0)) if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
usage_with_options(pack_refs_usage, opts); usage_with_options(pack_refs_usage, opts);
return pack_refs(flags); return pack_refs(flags);
} }

View File

@ -142,7 +142,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
save_commit_buffer = 0; save_commit_buffer = 0;
init_revisions(&revs, prefix); init_revisions(&revs, prefix);
argc = parse_options(argc, argv, options, prune_usage, 0); argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
while (argc--) { while (argc--) {
unsigned char sha1[20]; unsigned char sha1[20];
const char *name = *argv++; const char *name = *argv++;

View File

@ -198,7 +198,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, push_usage, 0); argc = parse_options(argc, argv, prefix, options, push_usage, 0);
if (tags) if (tags)
add_refspec("refs/tags/*"); add_refspec("refs/tags/*");

View File

@ -79,7 +79,8 @@ static int add(int argc, const char **argv)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, builtin_remote_usage, 0); argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
0);
if (argc < 2) if (argc < 2)
usage_with_options(builtin_remote_usage, options); usage_with_options(builtin_remote_usage, options);
@ -986,7 +987,8 @@ static int show(int argc, const char **argv)
struct string_list info_list = { NULL, 0, 0, 0 }; struct string_list info_list = { NULL, 0, 0, 0 };
struct show_info info; struct show_info info;
argc = parse_options(argc, argv, options, builtin_remote_usage, 0); argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
0);
if (argc < 1) if (argc < 1)
return show_all(); return show_all();
@ -1076,7 +1078,8 @@ static int set_head(int argc, const char **argv)
"delete refs/remotes/<name>/HEAD"), "delete refs/remotes/<name>/HEAD"),
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, builtin_remote_usage, 0); argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
0);
if (argc) if (argc)
strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]); strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
@ -1130,7 +1133,8 @@ static int prune(int argc, const char **argv)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, builtin_remote_usage, 0); argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
0);
if (argc < 1) if (argc < 1)
usage_with_options(builtin_remote_usage, options); usage_with_options(builtin_remote_usage, options);
@ -1220,7 +1224,7 @@ static int update(int argc, const char **argv)
OPT_END() OPT_END()
}; };
argc = parse_options(argc, argv, options, builtin_remote_usage, argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
PARSE_OPT_KEEP_ARGV0); PARSE_OPT_KEEP_ARGV0);
if (argc < 2) { if (argc < 2) {
argc = 2; argc = 2;
@ -1306,7 +1310,7 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
}; };
int result; int result;
argc = parse_options(argc, argv, options, builtin_remote_usage, argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
PARSE_OPT_STOP_AT_NON_OPTION); PARSE_OPT_STOP_AT_NON_OPTION);
if (argc < 1) if (argc < 1)

View File

@ -203,7 +203,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, options, git_reset_usage, argc = parse_options(argc, argv, prefix, options, git_reset_usage,
PARSE_OPT_KEEP_DASHDASH); PARSE_OPT_KEEP_DASHDASH);
reflog_action = args_to_str(argv); reflog_action = args_to_str(argv);
setenv("GIT_REFLOG_ACTION", reflog_action, 0); setenv("GIT_REFLOG_ACTION", reflog_action, 0);

View File

@ -318,7 +318,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
int onb = 0, osz = 0, unb = 0, usz = 0; int onb = 0, osz = 0, unb = 0, usz = 0;
strbuf_addstr(&parsed, "set --"); strbuf_addstr(&parsed, "set --");
argc = parse_options(argc, argv, parseopt_opts, parseopt_usage, argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
PARSE_OPT_KEEP_DASHDASH); PARSE_OPT_KEEP_DASHDASH);
if (argc < 1 || strcmp(argv[0], "--")) if (argc < 1 || strcmp(argv[0], "--"))
usage_with_options(parseopt_usage, parseopt_opts); usage_with_options(parseopt_usage, parseopt_opts);
@ -393,7 +393,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
/* put an OPT_END() */ /* put an OPT_END() */
ALLOC_GROW(opts, onb + 1, osz); ALLOC_GROW(opts, onb + 1, osz);
memset(opts + onb, 0, sizeof(opts[onb])); memset(opts + onb, 0, sizeof(opts[onb]));
argc = parse_options(argc, argv, opts, usage, argc = parse_options(argc, argv, prefix, opts, usage,
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
strbuf_addf(&parsed, " --"); strbuf_addf(&parsed, " --");

View File

@ -60,7 +60,7 @@ static void parse_args(int argc, const char **argv)
OPT_END(), OPT_END(),
}; };
if (parse_options(argc, argv, options, usage_str, 0) != 1) if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
usage_with_options(usage_str, options); usage_with_options(usage_str, options);
arg = argv[0]; arg = argv[0];

View File

@ -157,7 +157,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0); argc = parse_options(argc, argv, prefix, builtin_rm_options,
builtin_rm_usage, 0);
if (!argc) if (!argc)
usage_with_options(builtin_rm_usage, builtin_rm_options); usage_with_options(builtin_rm_usage, builtin_rm_options);

View File

@ -263,7 +263,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
shortlog_init(&log); shortlog_init(&log);
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH | parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
PARSE_OPT_KEEP_ARGV0); PARSE_OPT_KEEP_ARGV0);
for (;;) { for (;;) {

View File

@ -697,7 +697,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
av = default_arg - 1; /* ick; we would not address av[0] */ av = default_arg - 1; /* ick; we would not address av[0] */
} }
ac = parse_options(ac, av, builtin_show_branch_options, ac = parse_options(ac, av, prefix, builtin_show_branch_options,
show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION); show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
if (all_heads) if (all_heads)
all_remotes = 1; all_remotes = 1;

View File

@ -36,7 +36,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
}; };
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, options, git_symbolic_ref_usage, 0); argc = parse_options(argc, argv, prefix, options,
git_symbolic_ref_usage, 0);
if (msg &&!*msg) if (msg &&!*msg)
die("Refusing to perform update with empty message"); die("Refusing to perform update with empty message");
switch (argc) { switch (argc) {

View File

@ -405,7 +405,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
git_config(git_tag_config, NULL); git_config(git_tag_config, NULL);
argc = parse_options(argc, argv, options, git_tag_usage, 0); argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
msgfile = parse_options_fix_filename(prefix, msgfile); msgfile = parse_options_fix_filename(prefix, msgfile);
if (keyid) { if (keyid) {

View File

@ -23,7 +23,8 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
}; };
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
argc = parse_options(argc, argv, options, git_update_ref_usage, 0); argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
0);
if (msg && !*msg) if (msg && !*msg)
die("Refusing to perform update with empty message."); die("Refusing to perform update with empty message.");

View File

@ -84,7 +84,8 @@ int main(int argc, const char **argv)
git_extract_argv0_path(argv[0]); git_extract_argv0_path(argv[0]);
argc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0); argc = parse_options(argc, argv, NULL, hash_object_options,
hash_object_usage, 0);
if (write_object) { if (write_object) {
prefix = setup_git_directory(); prefix = setup_git_directory();

View File

@ -285,12 +285,14 @@ static void check_typos(const char *arg, const struct option *options)
} }
void parse_options_start(struct parse_opt_ctx_t *ctx, void parse_options_start(struct parse_opt_ctx_t *ctx,
int argc, const char **argv, int flags) int argc, const char **argv, const char *prefix,
int flags)
{ {
memset(ctx, 0, sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx));
ctx->argc = argc - 1; ctx->argc = argc - 1;
ctx->argv = argv + 1; ctx->argv = argv + 1;
ctx->out = argv; ctx->out = argv;
ctx->prefix = prefix;
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
ctx->flags = flags; ctx->flags = flags;
if ((flags & PARSE_OPT_KEEP_UNKNOWN) && if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
@ -389,12 +391,13 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
return ctx->cpidx + ctx->argc; return ctx->cpidx + ctx->argc;
} }
int parse_options(int argc, const char **argv, const struct option *options, int parse_options(int argc, const char **argv, const char *prefix,
const char * const usagestr[], int flags) const struct option *options, const char * const usagestr[],
int flags)
{ {
struct parse_opt_ctx_t ctx; struct parse_opt_ctx_t ctx;
parse_options_start(&ctx, argc, argv, flags); parse_options_start(&ctx, argc, argv, prefix, flags);
switch (parse_options_step(&ctx, options, usagestr)) { switch (parse_options_step(&ctx, options, usagestr)) {
case PARSE_OPT_HELP: case PARSE_OPT_HELP:
exit(129); exit(129);

View File

@ -122,7 +122,7 @@ struct option {
* non-option arguments in argv[]. * non-option arguments in argv[].
* Returns the number of arguments left in argv[]. * Returns the number of arguments left in argv[].
*/ */
extern int parse_options(int argc, const char **argv, extern int parse_options(int argc, const char **argv, const char *prefix,
const struct option *options, const struct option *options,
const char * const usagestr[], int flags); const char * const usagestr[], int flags);
@ -148,13 +148,15 @@ struct parse_opt_ctx_t {
int argc, cpidx; int argc, cpidx;
const char *opt; const char *opt;
int flags; int flags;
const char *prefix;
}; };
extern int parse_options_usage(const char * const *usagestr, extern int parse_options_usage(const char * const *usagestr,
const struct option *opts); const struct option *opts);
extern void parse_options_start(struct parse_opt_ctx_t *ctx, extern void parse_options_start(struct parse_opt_ctx_t *ctx,
int argc, const char **argv, int flags); int argc, const char **argv, const char *prefix,
int flags);
extern int parse_options_step(struct parse_opt_ctx_t *ctx, extern int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options, const struct option *options,

View File

@ -65,7 +65,7 @@ int main(int argc, const char **argv)
}; };
int i; int i;
argc = parse_options(argc, argv, options, usage, 0); argc = parse_options(argc, argv, NULL, options, usage, 0);
printf("boolean: %d\n", boolean); printf("boolean: %d\n", boolean);
printf("integer: %u\n", integer); printf("integer: %u\n", integer);