git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/4] stripspace: Use parse-options for command-line parsing

From
Tobias Klauser <tklauser@distanz.ch>
Date
Oct 17, 2015, 10:30 UTC
Message-ID
<20151017103032.GC2468@distanz.ch>
In-Reply-To
<xmqqoafyg2sp.fsf@gitster.mtv.corp.google.com>
On 2015-10-16 at 19:07:34 +0200, Junio C Hamano <gitster@pobox.com> wrote:
Show 11 quoted lines
> Tobias Klauser <tklauser@distanz.ch> writes:
> 
> > 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".

I didn't even know about that feature, but now that you've mentioned it I will certainly make use of it more in the future :) And of course, I'll adjust the commit message accordingly for v3.

Show 34 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?

Agreed, we should check it. I'll go with the implementation you suggested in the follow-up message.

Show 17 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?
Likewise, it shouldn't be there as in your follow-up patch.
Previous: Junio C HamanoNext: Tobias Klauser
Message 10 of 23 in “stripspace: Implement and use --count-lines option”
  1. 0/4 stripspace: Implement and use --count-lines optionTobias Klauser, Oct 16, 2015
  2. 1/4 strbuf: make stripspace() part of strbufTobias Klauser, Oct 16, 2015
  3. 2/4 stripspace: Use parse-options for command-line parsingTobias Klauser, Oct 16, 2015
  4. Junio C HamanoOct 16, 2015
  5. Junio C HamanoOct 16, 2015
  6. Tobias KlauserOct 17, 2015
  7. Junio C HamanoOct 17, 2015
  8. Tobias KlauserOct 20, 2015
  9. Junio C HamanoOct 20, 2015
  10. Tobias KlauserOct 17, 2015
  11. 3/4 stripspace: Implement --count-lines optionTobias Klauser, Oct 16, 2015
  12. Eric SunshineOct 17, 2015
  13. Junio C HamanoOct 18, 2015
  14. Tobias KlauserOct 19, 2015
  15. Christian CouderOct 19, 2015
  16. Eric SunshineOct 19, 2015
  17. Matthieu MoyOct 19, 2015
  18. Tobias KlauserOct 19, 2015
  19. 4/4 git rebase -i: Use newly added --count-lines option for stripspaceTobias Klauser, Oct 16, 2015
  20. Junio C HamanoOct 16, 2015
  21. Tobias KlauserOct 17, 2015
  22. Matthieu MoyOct 16, 2015
  23. Tobias KlauserOct 17, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.