From: Ian Campbell Subject: Re: [PATCH 3 of 3 V4] xl: SWITCH_FOREACH_OPT handles special options directly Date: Thu, 3 Jan 2013 16:27:58 +0000 Message-ID: <1357230478.10985.156.camel@zakaz.uk.xensource.com> References: <0d2f4198f2c3fd6574e9.1357213183@cosworth.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <0d2f4198f2c3fd6574e9.1357213183@cosworth.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: "xen-devel@lists.xen.org" Cc: Ian Jackson List-Id: xen-devel@lists.xenproject.org On Thu, 2013-01-03 at 11:39 +0000, Ian Campbell wrote: > # HG changeset patch > # User Ian Campbell > # Date 1357213146 0 > # Node ID 0d2f4198f2c3fd6574e9058a528ac5530332b14e > # Parent 1a1a2fa73670fd55004505b18a2b31418dcf447a > xl: SWITCH_FOREACH_OPT handles special options directly. > > This removes the need for the "case 0: case 2:" boilerplate in every > main_foo(). Calls exit(3) directly which is OK since xl cleans up the > context etc in an atexit(3) handler. > > Signed-off-by: Ian Campbell > -- > v4: exit() directly instead of returning. > > diff -r 1a1a2fa73670 -r 0d2f4198f2c3 tools/libxl/xl_cmdimpl.c > --- a/tools/libxl/xl_cmdimpl.c Thu Jan 03 11:38:48 2013 +0000 > +++ b/tools/libxl/xl_cmdimpl.c Thu Jan 03 11:39:06 2013 +0000 > @@ -2396,12 +2396,13 @@ static int def_getopt(int argc, char * c > * `lopts`) should be handled by a case statement as if it were inside > * a switch statement. > * > - * In addition to the options provided in opts callers must handle > - * two additional pseudo options: > - * 0 -- generated if the user passes a -h option. help will be printed, > - * caller should immediately return 0. > - * 2 -- generated if the user does not provided `num_required_opts` > - * non-option arguments, caller should immediately return 2. > + * In addition to the options provided in opts the macro will handle > + * the "help" option and enforce a minimum number of non-option > + * command line pearameters as follows: > + * -- if the user passes a -h or --help option. help will be printed, > + * and the macro will cause the process to exit with code 0. > + * -- if the user does not provided `num_required_opts` non-option > + * arguments, the macro will cause the process to exit with code 2. > * > * Example: > * > @@ -2409,8 +2410,6 @@ static int def_getopt(int argc, char * c > * int opt; > * > * SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) { > - * case 0: case2: > - * return opt; > * case 'b': > * ... handle b option... > * break; > @@ -2426,6 +2425,8 @@ static int def_getopt(int argc, char * c > commandname, num_required_opts) \ > while (((opt) = def_getopt(argc, argv, (opts), (longopts), \ > (commandname), (num_required_opts))) != -1) \ > + if (opt == 0) exit(0); \ > + if (opt == 2) exit(2); \ > switch (opt) This is bogus due to the lack of a { after the while which leads to an infinite loop (the original case I tested was lucky enough not to hit it...) It's obviously a brain fart anyway -- I should push the exits down into def_getopt... Ian.