summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2015-10-16 17:16:43 +0200
committerJunio C Hamano <gitster@pobox.com>2015-10-16 10:28:49 -0700
commitbed4452468cc564adc5ab99dfdbee0eb0f7cfb97 (patch)
tree97c1783419526e372bb6c29de6ba2e9192882cf3
parent63af4a8446a624ad0d83d9b766c0eb5fbf83be12 (diff)
downloadgit-tk/stripspace.tar.gz
stripspace: use parse-options for command-line parsingtk/stripspace
Use parse-options to parse command-line options instead of a hand-crafted implementation. The users can now use a unique prefix of the long option to say e.g. "git stripspace --strip". Signed-off-by: Tobias Klauser <tklauser@distanz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin/stripspace.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index f6770937bb..a8b7a93b43 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -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);