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

Re: [PATCH 05/11] revert: Catch incompatible command-line options early

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
May 8, 2011, 11:47 UTC
Message-ID
<20110508114749.GA3114@ramkum.desktop.amazon.com>
In-Reply-To
<7vfwpotqdx.fsf@alter.siamese.dyndns.org>
Hi Junio,
Junio C Hamano writes:
Show 38 quoted lines
> Ramkumar Ramachandra <artagnon@gmail.com> writes:
> > @@ -112,6 +130,13 @@ static void parse_args(int argc, const char **argv)
> >  	if (cmd_opts.commit_argc < 2)
> >  		usage_with_options(usage_str, options);
> >  
> > +	if (cmd_opts.allow_ff) {
> > +		int opt_bitarray[] = {cmd_opts.signoff, cmd_opts.no_commit,
> > +				      cmd_opts.no_replay, cmd_opts.edit};
> > +		die_opt_incompatible(me, "--ff", 4, opt_bitarray, "--signoff",
> > +				"--no-commit", "-x", "--edit");
> > +	}
> 
> Why not do it like this instead?
> 
> 	struct incompatible {
>         	unsigned option_bit;
>                 const char *option_name;
> 	} incompatible[] = {
> 		{ opts->signoff, "--signoff" },
>                 { opts->no_commit, "--no-commit" },
>                 ...
> 	};
> 	verify_compatible("me", "--ff", incompatible, ARRAY_SIZE(incompatible));
> 
> Or if you are shooting for ease-of-use, it might make sense to do it like
> this:
> 
> 	verify_compatible("me", "--ff",
>         		"--signoff", opts->signoff,
>                         "--no-commit", opts->no_commit,
>                         ...
>                         NULL);
> 
> and make verify_compatible() a varargs function that takes two optional
> arguments at a time, i.e. const char *, followed by an int.  Then there is
> no need for extra "int opt_bitarray[]" or "struct incompatible".
> 
> That would justify use of varargs, I think.

Now that you point it out, my original approach was unnecessarily cryptic and convoluted. I've followed this approach in my new series, and kept varargs -- the code looks much prettier now. Thanks :)

I can't justify changing the name from "die_opt_incompatible" to "verify_compatible" though; the name I've chosen seems to be more appropriate/ descriptive. Further, I think command-line parsing should always be the toplevel caller, and "die" is therefore more appropriate than "return error" here.

-- Ram
Previous: Junio C HamanoNext: Ramkumar Ramachandra
Message 17 of 36 in “Sequencer Foundations”
  1. 00/11 Sequencer FoundationsRamkumar Ramachandra, Apr 10, 2011
  2. 01/11 revert: Avoid calling die; return error insteadRamkumar Ramachandra, Apr 10, 2011
  3. Jonathan NiederApr 10, 2011
  4. Ramkumar RamachandraMay 8, 2011
  5. Junio C HamanoApr 11, 2011
  6. 02/11 revert: Lose global variables "commit" and "me"Ramkumar Ramachandra, Apr 10, 2011
  7. Christian CouderApr 11, 2011
  8. Ramkumar RamachandraApr 11, 2011
  9. 03/11 revert: Introduce a struct to parse command-line options intoRamkumar Ramachandra, Apr 10, 2011
  10. Jonathan NiederApr 10, 2011
  11. Ramkumar RamachandraMay 8, 2011
  12. Junio C HamanoApr 11, 2011
  13. Ramkumar RamachandraMay 8, 2011
  14. 04/11 revert: Separate cmdline argument handling from the functional codeRamkumar Ramachandra, Apr 10, 2011
  15. 05/11 revert: Catch incompatible command-line options earlyRamkumar Ramachandra, Apr 10, 2011
  16. Junio C HamanoApr 11, 2011
  17. Ramkumar RamachandraMay 8, 2011
  18. 06/11 revert: Implement parsing --continue, --abort and --skipRamkumar Ramachandra, Apr 10, 2011
  19. 07/11 revert: Handle conflict resolutions more elegantlyRamkumar Ramachandra, Apr 10, 2011
  20. 08/11 usage: Introduce error_errno correspoding to die_errnoRamkumar Ramachandra, Apr 10, 2011
  21. 09/11 revert: Write head, todo, done filesRamkumar Ramachandra, Apr 10, 2011
  22. 10/11 revert: Give noop a default value while argument parsingRamkumar Ramachandra, Apr 10, 2011
  23. 11/11 revert: Implement --abort processingRamkumar Ramachandra, Apr 10, 2011
  24. Daniel BarkalowApr 10, 2011
  25. Ramkumar RamachandraApr 11, 2011
  26. Jonathan NiederApr 10, 2011
  27. Daniel BarkalowApr 11, 2011
  28. Jonathan NiederApr 11, 2011
  29. Ramkumar RamachandraApr 11, 2011
  30. Christian CouderApr 11, 2011
  31. Ramkumar RamachandraApr 11, 2011
  32. Christian CouderApr 11, 2011
  33. Ramkumar RamachandraApr 11, 2011
  34. Daniel BarkalowApr 11, 2011
  35. Jonathan NiederApr 11, 2011
  36. Daniel BarkalowApr 11, 2011

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.