Merge branch 'ld/p4-import-labels' into maint
[gitweb.git] / builtin / stripspace.c
index f6770937bb0c531dcd7f07c11c999a7bf96643f1..a8b7a93b43e414b51c76a4853ff3b49394ab1d77 100644 (file)
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "parse-options.h"
 #include "strbuf.h"
 
 static void comment_lines(struct strbuf *buf)
@@ -12,41 +13,45 @@ static void comment_lines(struct strbuf *buf)
        free(msg);
 }
 
-static const char *usage_msg = "\n"
-"  git stripspace [-s | --strip-comments] < input\n"
-"  git stripspace [-c | --comment-lines] < input";
+static const char * const stripspace_usage[] = {
+       N_("git stripspace [-s | --strip-comments] < input"),
+       N_("git stripspace [-c | --comment-lines] < input"),
+       NULL
+};
+
+enum stripspace_mode {
+       STRIP_DEFAULT = 0,
+       STRIP_COMMENTS,
+       COMMENT_LINES
+};
 
 int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
        struct strbuf buf = STRBUF_INIT;
-       int strip_comments = 0;
-       enum { INVAL = 0, STRIP_SPACE = 1, COMMENT_LINES = 2 } mode = STRIP_SPACE;
-
-       if (argc == 2) {
-               if (!strcmp(argv[1], "-s") ||
-                   !strcmp(argv[1], "--strip-comments")) {
-                       strip_comments = 1;
-               } else if (!strcmp(argv[1], "-c") ||
-                          !strcmp(argv[1], "--comment-lines")) {
-                       mode = COMMENT_LINES;
-               } else {
-                       mode = INVAL;
-               }
-       } else if (argc > 1) {
-               mode = INVAL;
-       }
-
-       if (mode == INVAL)
-               usage(usage_msg);
-
-       if (strip_comments || mode == COMMENT_LINES)
+       enum stripspace_mode mode = STRIP_DEFAULT;
+
+       const struct option options[] = {
+               OPT_CMDMODE('s', "strip-comments", &mode,
+                           N_("skip and remove all lines starting with comment character"),
+                           STRIP_COMMENTS),
+               OPT_CMDMODE('c', "comment-lines", &mode,
+                           N_("prepend comment character and blank to each line"),
+                           COMMENT_LINES),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
+       if (argc)
+               usage_with_options(stripspace_usage, options);
+
+       if (mode == STRIP_COMMENTS || mode == COMMENT_LINES)
                git_config(git_default_config, NULL);
 
        if (strbuf_read(&buf, 0, 1024) < 0)
                die_errno("could not read the input");
 
-       if (mode == STRIP_SPACE)
-               strbuf_stripspace(&buf, strip_comments);
+       if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
+               strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
        else
                comment_lines(&buf);