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:

committed by
Junio C Hamano

parent
63af4a8446
commit
bed4452468
@ -1,5 +1,6 @@
|
|||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
#include "parse-options.h"
|
||||||
#include "strbuf.h"
|
#include "strbuf.h"
|
||||||
|
|
||||||
static void comment_lines(struct strbuf *buf)
|
static void comment_lines(struct strbuf *buf)
|
||||||
@ -12,41 +13,45 @@ static void comment_lines(struct strbuf *buf)
|
|||||||
free(msg);
|
free(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *usage_msg = "\n"
|
static const char * const stripspace_usage[] = {
|
||||||
" git stripspace [-s | --strip-comments] < input\n"
|
N_("git stripspace [-s | --strip-comments] < input"),
|
||||||
" git stripspace [-c | --comment-lines] < 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)
|
int cmd_stripspace(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
struct strbuf buf = STRBUF_INIT;
|
struct strbuf buf = STRBUF_INIT;
|
||||||
int strip_comments = 0;
|
enum stripspace_mode mode = STRIP_DEFAULT;
|
||||||
enum { INVAL = 0, STRIP_SPACE = 1, COMMENT_LINES = 2 } mode = STRIP_SPACE;
|
|
||||||
|
|
||||||
if (argc == 2) {
|
const struct option options[] = {
|
||||||
if (!strcmp(argv[1], "-s") ||
|
OPT_CMDMODE('s', "strip-comments", &mode,
|
||||||
!strcmp(argv[1], "--strip-comments")) {
|
N_("skip and remove all lines starting with comment character"),
|
||||||
strip_comments = 1;
|
STRIP_COMMENTS),
|
||||||
} else if (!strcmp(argv[1], "-c") ||
|
OPT_CMDMODE('c', "comment-lines", &mode,
|
||||||
!strcmp(argv[1], "--comment-lines")) {
|
N_("prepend comment character and blank to each line"),
|
||||||
mode = COMMENT_LINES;
|
COMMENT_LINES),
|
||||||
} else {
|
OPT_END()
|
||||||
mode = INVAL;
|
};
|
||||||
}
|
|
||||||
} else if (argc > 1) {
|
|
||||||
mode = INVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode == INVAL)
|
argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
|
||||||
usage(usage_msg);
|
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);
|
git_config(git_default_config, NULL);
|
||||||
|
|
||||||
if (strbuf_read(&buf, 0, 1024) < 0)
|
if (strbuf_read(&buf, 0, 1024) < 0)
|
||||||
die_errno("could not read the input");
|
die_errno("could not read the input");
|
||||||
|
|
||||||
if (mode == STRIP_SPACE)
|
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
|
||||||
strbuf_stripspace(&buf, strip_comments);
|
strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
|
||||||
else
|
else
|
||||||
comment_lines(&buf);
|
comment_lines(&buf);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user