From c36979d964f582a51e2b6e5c8d82d7974218efd1 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Fri, 21 Mar 2008 17:27:28 +0100 Subject: [PATCH] Simplify subcommand option parsing by moving it out of the loop --- tig.c | 53 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/tig.c b/tig.c index 8fb2d4c..f8132fd 100644 --- a/tig.c +++ b/tig.c @@ -496,32 +496,33 @@ parse_options(int argc, char *argv[]) { char *altargv[1024]; int altargc = 0; - char *subcommand = NULL; + char *subcommand; int i; - for (i = 1; i < argc; i++) { - char *opt = argv[i]; + if (argc <= 1) + return TRUE; - if (!strcmp(opt, "log") || - !strcmp(opt, "diff")) { - subcommand = opt; - opt_request = opt[0] == 'l' - ? REQ_VIEW_LOG : REQ_VIEW_DIFF; - warn("`tig %s' has been deprecated", opt); - break; - } + subcommand = argv[1]; + if (!strcmp(subcommand, "status")) { + opt_request = REQ_VIEW_STATUS; + if (argc > 2) + warn("ignoring arguments after `%s'", subcommand); + return TRUE; - if (!strcmp(opt, "show")) { - subcommand = opt; - opt_request = REQ_VIEW_DIFF; - break; - } + } else if (!strcmp(subcommand, "show")) { + opt_request = REQ_VIEW_DIFF; - if (!strcmp(opt, "status")) { - subcommand = opt; - opt_request = REQ_VIEW_STATUS; - break; - } + } else if (!strcmp(subcommand, "log") || !strcmp(subcommand, "diff")) { + opt_request = subcommand[0] == 'l' + ? REQ_VIEW_LOG : REQ_VIEW_DIFF; + warn("`tig %s' has been deprecated", subcommand); + + } else { + subcommand = NULL; + } + + for (i = 1 + !!subcommand; i < argc; i++) { + char *opt = argv[i]; if (opt[0] && opt[0] != '-') break; @@ -565,18 +566,10 @@ parse_options(int argc, char *argv[]) warn("`%s' has been deprecated", opt); } - /* Check that no 'alt' arguments occured before a subcommand. */ - if (subcommand && i < argc && altargc > 0) - die("unknown arguments before `%s'", argv[i]); - if (!isatty(STDIN_FILENO)) { opt_request = REQ_VIEW_PAGER; opt_pipe = stdin; - } else if (opt_request == REQ_VIEW_STATUS) { - if (argc - i > 1) - warn("ignoring arguments after `%s'", argv[i]); - } else if (i < argc || altargc > 0) { int alti = 0; size_t buf_size; @@ -586,7 +579,7 @@ parse_options(int argc, char *argv[]) * options required for the main view parser. */ string_copy(opt_cmd, "git log --no-color --pretty=raw --boundary --parents"); else - string_copy(opt_cmd, "git"); + string_format(opt_cmd, "git %s", subcommand); buf_size = strlen(opt_cmd); while (buf_size < sizeof(opt_cmd) && alti < altargc) { -- 2.30.2