Re: [PATCH v2 2/4] stripspace: Use parse-options for command-line parsing
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 16, 2015, 17:07 UTC
- Message-ID
- <xmqqoafyg2sp.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <1445008605-16534-3-git-send-email-tklauser@distanz.ch>
Tobias Klauser <tklauser@distanz.ch> writes:
Show 5 quoted lines
> Use parse-options to parse command-line options instead of a > hand-crafted implementation. > > This is a preparatory patch to simplify the introduction of the > --count-lines option in a follow-up patch.
The second paragraph is probably of much lessor importance than one thing you forgot to mention: the users can now use a unique prefix of the option and say "stripspace --comment".
Show 28 quoted lines
> +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);When given "git stripspace -s blorg", we used to set mode to INVAL and then showed the correct usage. But we no longer have a check that corresponds to the old INVAL thing, do we? Perhaps check argc to detect presence of an otherwise ignored non-option argument immediately after parse_options() returns?
Show 15 quoted lines
> - 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,
> + PARSE_OPT_KEEP_DASHDASH);What is the point of keep-dashdash here?