1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-10 11:06:09 +02:00

stripspace: use parse-options for command-line parsing

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>
This commit is contained in:
Tobias Klauser 2015-10-16 17:16:43 +02:00 committed by Junio C Hamano
parent 63af4a8446
commit bed4452468

View 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;
enum stripspace_mode mode = STRIP_DEFAULT;
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;
}
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()
};
if (mode == INVAL)
usage(usage_msg);
argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
if (argc)
usage_with_options(stripspace_usage, options);
if (strip_comments || mode == COMMENT_LINES)
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);