multi-pack-index: simplify handling of unknown --options
PATCH 1/1 multi-pack-index: simplify handling of unknown --options
Although parse_options() can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invoke parse_options() with the PARSE_OPT_KEEP_UNKNOWN flag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.
Let parse_options() handle unknown options instead, which, besides simpler code, has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.
--- builtin/multi-pack-index.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
builtin/multi-pack-index.c +4 -16
@@ -134,10 +134,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_write_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_write_usage, - options); +0 );FREE_AND_NULL (options);
@@ -176,10 +173,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_verify_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_verify_usage, - options); +0 );FREE_AND_NULL (options);
@@ -202,10 +196,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_expire_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_expire_usage, - options); +0 );FREE_AND_NULL (options);
@@ -232,10 +223,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv) argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_repack_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_repack_usage, - options); +0 );FREE_AND_NULL (options);
Signature · 1 line
2.37.0.340.g5e8d960d32
Quoted 5 lines · SZEDER Gábor
Although
parse_options()can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invokeparse_options()with thePARSE_OPT_KEEP_UNKNOWNflag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.
The existing code check if there are any unparsed arguments or options.
Omitting PARSE_OPT_KEEP_UNKNOWN allows parse_options() to deal with unknown options by complaining, but it happily leaves non-options on the command line and reports how many of them there are.
Doesn't this patch make
$ git multi-pack-index write what-is-this-extra-arg-doing-here
silently ignore the extra argument instead of barfing on it?
Quoted 3 lines
Let
parse_options()handle unknown options instead, which, besides simpler code, has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.
Yes, I agree that getting rid of KEEP_UNKNOWN is a very good idea for this reason. But I suspect that we still need the "did we get an extra argument we do not know what to do with?" check.
Thanks.
Quoted 56 lines
Signed-off-by SZEDER Gábor <[email protected]>--- builtin/multi-pack-index.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)builtin/multi-pack-index.c +4
-16@@ -134,10 +134,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_write_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_write_usage, - options); +0 );FREE_AND_NULL (options);@@ -176,10 +173,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_verify_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_verify_usage, - options); +0 );FREE_AND_NULL (options);@@ -202,10 +196,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_expire_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_expire_usage, - options); +0 );FREE_AND_NULL (options);@@ -232,10 +223,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv) argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_repack_usage, -PARSE_OPT_KEEP_UNKNOWN ); -if (argc) -usage_with_options (builtin_multi_pack_index_repack_usage, - options); +0 );FREE_AND_NULL (options);
Quoted 21 lines · Junio C Hamano
Quoted 5 lines · SZEDER Gábor
Although
parse_options()can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invokeparse_options()with thePARSE_OPT_KEEP_UNKNOWNflag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.The existing code check if there are any unparsed arguments or options.
Omitting
PARSE_OPT_KEEP_UNKNOWNallowsparse_options()to deal with unknown options by complaining, but it happily leaves non-options on the command line and reports how many of them there are.Doesn't this patch make
$ git multi-pack-index write what-is-this-extra-arg-doing-heresilently ignore the extra argument instead of barfing on it?
Quoted 7 lines
Quoted 3 lines
Let
parse_options()handle unknown options instead, which, besides simpler code, has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.Yes, I agree that getting rid of
KEEP_UNKNOWNis a very good idea for this reason. But I suspect that we still need the "did we get an extra argument we do not know what to do with?" check.
Uh, indeed. I got too trigger-happy with deleting lines. Updated patch below.
--- >8 ---
Subject: multi-pack-index: simplify handling of unknown --options
Although parse_options() can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invoke parse_options() with the PARSE_OPT_KEEP_UNKNOWN flag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.
Drop that PARSE_OPT_KEEP_UNKNOWN flag to let parse_options() handle unknown options instead, which has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.
Do leave the unparsed arguments check to catch any unexpected non-option arguments, though, e.g. 'git multi-pack-index write foo'.
--- builtin/multi-pack-index.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
builtin/multi-pack-index.c +4 -4
@@ -134,7 +134,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_write_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_write_usage, options);
@@ -176,7 +176,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_verify_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_verify_usage, options);
@@ -202,7 +202,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_expire_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_expire_usage, options);
@@ -232,7 +232,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv) argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_repack_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_repack_usage, options);
Signature · 1 line
2.37.0.340.g5e8d960d32
Quoted 88 lines · SZEDER Gábor
Quoted 21 lines · Junio C Hamano
Quoted 5 lines · SZEDER Gábor
Although
parse_options()can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invokeparse_options()with thePARSE_OPT_KEEP_UNKNOWNflag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.The existing code check if there are any unparsed arguments or options.
Omitting
PARSE_OPT_KEEP_UNKNOWNallowsparse_options()to deal with unknown options by complaining, but it happily leaves non-options on the command line and reports how many of them there are.Doesn't this patch make
$ git multi-pack-index write what-is-this-extra-arg-doing-heresilently ignore the extra argument instead of barfing on it?
Quoted 7 lines
Quoted 3 lines
Let
parse_options()handle unknown options instead, which, besides simpler code, has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.Yes, I agree that getting rid of
KEEP_UNKNOWNis a very good idea for this reason. But I suspect that we still need the "did we get an extra argument we do not know what to do with?" check.Uh, indeed. I got too trigger-happy with deleting lines. Updated patch below.
--- >8 ---
Subject: multi-pack-index: simplify handling of unknown --options
Although
parse_options()can handle unknown --options just fine, none of 'git multi-pack-index's subcommands rely on it, but do it on their
own: they invokeparse_options()with thePARSE_OPT_KEEP_UNKNOWNflag, then check whether there are any unparsed arguments left, and print usage and quit if necessary.Drop that
PARSE_OPT_KEEP_UNKNOWNflag to letparse_options()handle unknown options instead, which has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.Do leave the unparsed arguments check to catch any unexpected non-option arguments, though, e.g. 'git multi-pack-index write foo'.
Signed-off-by SZEDER Gábor <[email protected]>--- builtin/multi-pack-index.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)builtin/multi-pack-index.c +3
-3@@ -134,7 +134,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_write_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_write_usage, options);@@ -176,7 +176,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_verify_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_verify_usage, options);@@ -202,7 +202,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv) opts.flags |=MIDX_PROGRESS ; argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_expire_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_expire_usage, options);
builtin/multi-pack-index.c static int cmd_multi_pack_index_repack(int argc, const char **argv) · line 238
Quoted 8 lines
builtin/multi-pack-index.c +1
-1@@ -232,7 +232,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv) argc =parse_options (argc, argv,NULL , options, builtin_multi_pack_index_repack_usage, -PARSE_OPT_KEEP_UNKNOWN ); +0 );if (argc)usage_with_options (builtin_multi_pack_index_repack_usage, options);
Looks good, FWIW I've had this in my local tree for a long time (been meaning to submit it), which manages to delete the "argc" handling for this & others.
But I think just doing this is good for now (multi-pack-index isn't in the diff below, but I did it in another commit):
https://github.com/avar/git/commit/17b838ddd5a):
builtin/checkout--worker.c +2 -3
@@ -126,9 +126,8 @@ int cmd_checkout__worker(int argc, const char **argv, const char *prefix)git_config (git_default_config,NULL ); argc =parse_options (argc, argv, prefix, checkout_worker_options, - checkout_worker_usage,0 ); -if (argc >0 ) -usage_with_options (checkout_worker_usage, checkout_worker_options); + checkout_worker_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (state.base_dir) state.base_dir_len =strlen (state.base_dir);
builtin/column.c +2 -3
@@ -43,9 +43,8 @@ int cmd_column(int argc, const char **argv, const char *prefix)memset (&copts,0 ,sizeof (copts)); copts.padding =1 ; - argc =parse_options (argc, argv, prefix, options, builtin_column_usage,0 ); -if (argc) -usage_with_options (builtin_column_usage, options); +parse_options (argc, argv, prefix, options, builtin_column_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (real_command || command) {if (!real_command || !command ||strcmp (real_command, command))die (_ ("--command must be the first argument" ));
builtin/commit-graph.c +6 -10
@@ -80,11 +80,9 @@ static int graph_verify(int argc, const char **argv)trace2_cmd_mode ("verify" ); opts.progress =isatty (2 ); - argc =parse_options (argc, argv,NULL , - options, - builtin_commit_graph_verify_usage,0 ); -if (argc) -usage_with_options (builtin_commit_graph_verify_usage, options); +parse_options (argc, argv,NULL , options, + builtin_commit_graph_verify_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (!opts.obj_dir) opts.obj_dir =get_object_directory ();
@@ -241,11 +239,9 @@ static int graph_write(int argc, const char **argv)git_config (git_commit_graph_write_config, &opts); - argc =parse_options (argc, argv,NULL , - options, - builtin_commit_graph_write_usage,0 ); -if (argc) -usage_with_options (builtin_commit_graph_write_usage, options); +parse_options (argc, argv,NULL , options, + builtin_commit_graph_write_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (opts.reachable + opts.stdin_packs + opts.stdin_commits >1 )die (_ ("use at most one of --reachable, --stdin-commits, or --stdin-packs" ));
builtin/count-objects.c +2 -4
@@ -103,10 +103,8 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)git_config (git_default_config,NULL ); - argc =parse_options (argc, argv, prefix, opts, count_objects_usage,0 ); -/* we do not take arguments other than flags for now */ -if (argc) -usage_with_options (count_objects_usage, opts); +parse_options (argc, argv, prefix, opts, count_objects_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (verbose) { report_garbage = real_report_garbage;report_linked_checkout_garbage ();
builtin/fmt-merge-msg.c +1 -3
@@ -37,9 +37,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)git_config (fmt_merge_msg_config,NULL ); argc =parse_options (argc, argv, prefix, options, fmt_merge_msg_usage, -0 ); -if (argc >0 ) -usage_with_options (fmt_merge_msg_usage, options); +PARSE_OPT_ERROR_AT_NON_OPTION );if (shortlog_len <0 ) shortlog_len = (merge_log_config >0 ) ? merge_log_config :0 ;
builtin/gc.c +4 -7
@@ -587,9 +587,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix) pack_refs = !is_bare_repository (); argc =parse_options (argc, argv, prefix, builtin_gc_options, - builtin_gc_usage,0 ); -if (argc >0 ) -usage_with_options (builtin_gc_usage, builtin_gc_options); + builtin_gc_usage,PARSE_OPT_ERROR_AT_NON_OPTION );if (prune_expire &&parse_expiry_date (prune_expire, &dummy))die (_ ("failed to parse prune expiry value %s" ), prune_expire);
@@ -2491,10 +2489,9 @@ static int maintenance_start(int argc, const char **argv, const char *prefix) OPT_END() }; - argc =parse_options (argc, argv, prefix, options, - builtin_maintenance_start_usage,0 ); -if (argc) -usage_with_options (builtin_maintenance_start_usage, options); +parse_options (argc, argv, prefix, options, + builtin_maintenance_start_usage, +PARSE_OPT_ERROR_AT_NON_OPTION ); opts.scheduler =resolve_scheduler (opts.scheduler);validate_scheduler (opts.scheduler);
builtin/notes.c +2 -7
@@ -958,13 +958,8 @@ static int prune(int argc, const char **argv, const char *prefix) OPT_END() }; - argc =parse_options (argc, argv, prefix, options, git_notes_prune_usage, -0 ); - -if (argc) { -error (_ ("too many arguments" )); -usage_with_options (git_notes_prune_usage, options); - } +parse_options (argc, argv, prefix, options, git_notes_prune_usage, +PARSE_OPT_ERROR_AT_NON_OPTION ); t =init_notes_check ("prune" ,NOTES_INIT_WRITABLE );
builtin/stripspace.c +2 -3
@@ -42,9 +42,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) OPT_END() }; - argc =parse_options (argc, argv, prefix, options, stripspace_usage,0 ); -if (argc) -usage_with_options (stripspace_usage, options); +parse_options (argc, argv, prefix, options, stripspace_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );if (mode ==STRIP_COMMENTS || mode ==COMMENT_LINES ) {setup_git_directory_gently (&nongit);
builtin/worktree.c +5 -7
@@ -153,9 +153,8 @@ static int prune(int ac, const char **av, const char *prefix) }; expire =TIME_MAX ; - ac =parse_options (ac, av, prefix, options, worktree_usage,0 ); -if (ac) -usage_with_options (worktree_usage, options); +parse_options (ac, av, prefix, options, worktree_usage, +PARSE_OPT_ERROR_AT_NON_OPTION );prune_worktrees ();return 0 ; }
@@ -772,10 +771,9 @@ static int list(int ac, const char **av, const char *prefix) }; expire =TIME_MAX ; - ac =parse_options (ac, av, prefix, options, worktree_usage,0 ); -if (ac) -usage_with_options (worktree_usage, options); -else if (verbose && porcelain) +parse_options (ac, av, prefix, options, worktree_usage, +PARSE_OPT_ERROR_AT_NON_OPTION ); +if (verbose && porcelain)die (_ ("options '%s' and '%s' cannot be used together" ),"--verbose" ,"--porcelain" );else if (!line_terminator && !porcelain)die (_ ("the option '%s' requires '%s'" ),"-z" ,"--porcelain" );
imap-send.c +2 -4
@@ -1526,10 +1526,8 @@ int cmd_main(int argc, const char **argv)setup_git_directory_gently (&nongit_ok);git_config (git_imap_config,NULL ); - argc =parse_options (argc, (const char **)argv,"" , imap_send_options, imap_send_usage,0 ); - -if (argc) -usage_with_options (imap_send_usage, imap_send_options); +parse_options (argc, (const char **)argv,"" , imap_send_options, + imap_send_usage,PARSE_OPT_ERROR_AT_NON_OPTION );#ifndef USE_CURL_FOR_IMAP_SEND if (use_curl) {
parse-options.c +7 -0
@@ -631,6 +631,8 @@ void parse_options_start(struct parse_opt_ctx_t *ctx, ctx->out = argv; ctx->prefix = prefix; ctx->cpidx = ((flags &PARSE_OPT_KEEP_ARGV0 ) !=0 ); +if (flags &PARSE_OPT_ERROR_AT_NON_OPTION ) + flags |=PARSE_OPT_STOP_AT_NON_OPTION ; ctx->flags = flags;parse_options_alter_flags (ctx);parse_options_check_flags (options, ctx->flags);
@@ -894,6 +896,11 @@ int parse_options(int argc, const char **argv,case PARSE_OPT_COMPLETE :exit (0 );case PARSE_OPT_NON_OPTION : +if (flags &PARSE_OPT_ERROR_AT_NON_OPTION ) { +error (_ ("unknown non-option: `%s'" ), ctx.argv[0 ]); +usage_with_options (usagestr, options); + } +break ;case PARSE_OPT_DONE :break ;case PARSE_OPT_UNKNOWN :
parse-options.h +1 -0
@@ -34,6 +34,7 @@ enum parse_opt_flags {PARSE_OPT_SHELL_EVAL =1 <<6 ,PARSE_OPT_REV_PARSE_PARSEOPT =1 <<7 ,PARSE_OPT_INTERNAL_HELP_ON_ONE_ARG =1 <<8 , + PARSE_OPT_ERROR_AT_NON_OPTION = 1 << 9, };/**
t/helper/test-getcwd.c +2 -3
@@ -14,9 +14,8 @@ int cmd__getcwd(int argc, const char **argv) };char *cwd; - argc =parse_options (argc, argv,"test-tools" , options, getcwd_usage,0 ); -if (argc >0 ) -usage_with_options (getcwd_usage, options); + argc =parse_options (argc, argv,"test-tools" , options, getcwd_usage, +PARSE_OPT_ERROR_AT_NON_OPTION ); cwd =xgetcwd ();puts (cwd);
Quoted 10 lines · SZEDER Gábor
Quoted 7 lines
Quoted 3 lines
Let
parse_options()handle unknown options instead, which, besides simpler code, has the additional benefit that it prints not only the usage but an "error: unknown option `foo'" message as well.Yes, I agree that getting rid of
KEEP_UNKNOWNis a very good idea for this reason. But I suspect that we still need the "did we get an extra argument we do not know what to do with?" check.Uh, indeed. I got too trigger-happy with deleting lines. Updated patch below.
OK. I suspect that a test would have caught the breakage in the original. Would it make sense to add one now?
Thanks, will queue.