Code

Simplify subcommand option parsing by moving it out of the loop
authorJonas Fonseca <fonseca@diku.dk>
Fri, 21 Mar 2008 16:27:28 +0000 (17:27 +0100)
committerJonas Fonseca <fonseca@diku.dk>
Sat, 22 Mar 2008 20:20:51 +0000 (21:20 +0100)
tig.c

diff --git a/tig.c b/tig.c
index 8fb2d4c17526ffaa1bbea0261a75e805701326cf..f8132fd0f8aeaaf096aea74a0e2cd041edbfe2f7 100644 (file)
--- 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) {