git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Port builtin-add.c to use the new option parser.

From
Kristian Høgsberg <krh@redhat.com>
Date
Oct 3, 2007, 21:45 UTC
Message-ID
<1191447902-27326-2-git-send-email-krh@redhat.com>
In-Reply-To
<1191447902-27326-1-git-send-email-krh@redhat.com>
Signed-off-by: Kristian Høgsberg <krh@redhat.com>
---
 builtin-add.c |   64 ++++++++++++++++++--------------------------------------
 1 files changed, 21 insertions(+), 43 deletions(-)
diff --git a/builtin-add.c b/builtin-add.c
index 966e145..66fd99d 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -13,6 +13,7 @@
 #include "commit.h"
 #include "revision.h"
 #include "run-command.h"
+#include "parse-options.h"
 
 static const char builtin_add_usage[] =
 "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
@@ -160,21 +161,30 @@ static struct lock_file lock_file;
 static const char ignore_error[] =
 "The following paths are ignored by one of your .gitignore files:\n";
 
+static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
+static int add_interactive = 0;
+
+static struct option builtin_add_options[] = {
+	{ OPTION_BOOLEAN, "interactive", 'i', &add_interactive },
+	{ OPTION_BOOLEAN, NULL, 'n', &show_only },
+	{ OPTION_BOOLEAN, NULL, 'f', &ignored_too },
+	{ OPTION_BOOLEAN, NULL, 'v',&verbose },
+	{ OPTION_BOOLEAN, NULL, 'u',&take_worktree_changes },
+	{ OPTION_BOOLEAN, "refresh", 0, &refresh_only }
+};
+
 int cmd_add(int argc, const char **argv, const char *prefix)
 {
 	int i, newfd;
-	int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 	const char **pathspec;
 	struct dir_struct dir;
-	int add_interactive = 0;
 
-	for (i = 1; i < argc; i++) {
-		if (!strcmp("--interactive", argv[i]) ||
-		    !strcmp("-i", argv[i]))
-			add_interactive++;
-	}
+	i = parse_options(argc, argv, builtin_add_options,
+			  ARRAY_SIZE(builtin_add_options),
+			  builtin_add_usage);
+
 	if (add_interactive) {
-		if (argc != 2)
+		if (i > 0)
 			die("add --interactive does not take any parameters");
 		exit(interactive_add());
 	}
@@ -183,51 +193,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
 	newfd = hold_locked_index(&lock_file, 1);
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (arg[0] != '-')
-			break;
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-n")) {
-			show_only = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-f")) {
-			ignored_too = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-v")) {
-			verbose = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-u")) {
-			take_worktree_changes = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--refresh")) {
-			refresh_only = 1;
-			continue;
-		}
-		usage(builtin_add_usage);
-	}
-
 	if (take_worktree_changes) {
 		if (read_cache() < 0)
 			die("index file corrupt");
-		add_files_to_cache(verbose, prefix, argv + i);
+		add_files_to_cache(verbose, prefix, argv);
 		goto finish;
 	}
 
-	if (argc <= i) {
+	if (i == 0) {
 		fprintf(stderr, "Nothing specified, nothing added.\n");
 		fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 		return 0;
 	}
-	pathspec = get_pathspec(prefix, argv + i);
+	pathspec = get_pathspec(prefix, argv);
 
 	if (refresh_only) {
 		refresh(verbose, pathspec);
-- 
1.5.2.5
Previous: Kristian HøgsbergNext: Pierre Habouzit
Message 2 of 26 in “Add a simple option parser.”
  1. Add a simple option parser.Kristian Høgsberg, Oct 3, 2007
  2. Port builtin-add.c to use the new option parser.Kristian Høgsberg, Oct 3, 2007
  3. Pierre HabouzitOct 3, 2007
  4. Kristian HøgsbergOct 4, 2007
  5. Pierre HabouzitOct 4, 2007
  6. Pierre HabouzitOct 4, 2007
  7. Johannes SchindelinOct 4, 2007
  8. Pierre HabouzitOct 5, 2007
  9. Pierre HabouzitOct 5, 2007
  10. Add a simple option parser.Pierre Habouzit, Oct 5, 2007
  11. Mike HommeyOct 5, 2007
  12. Pierre HabouzitOct 5, 2007
  13. Medve Emilian-EMMEDVE1Oct 5, 2007
  14. Pierre HabouzitOct 5, 2007
  15. Medve Emilian-EMMEDVE1Oct 5, 2007
  16. David KastrupOct 5, 2007
  17. Pierre HabouzitOct 5, 2007
  18. Sven VerdoolaegeOct 6, 2007
  19. Linus TorvaldsOct 5, 2007
  20. Medve Emilian-EMMEDVE1Oct 5, 2007
  21. Pierre HabouzitOct 5, 2007
  22. Linus TorvaldsOct 5, 2007
  23. David KastrupOct 5, 2007
  24. Kristian HøgsbergOct 5, 2007
  25. Pierre HabouzitOct 5, 2007
  26. Pierre HabouzitOct 7, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.