Re: [PATCH 09/16] eject: use parse_switch()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Feb 22, 2015 at 02:41:39PM +0000, Sami Kerola wrote:
>  		case 'a':
>  			ctl->a_option = 1;
> -			if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
> -				ctl->a_arg = 0;
> -			else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
> -				ctl->a_arg = 1;
> -			else
> -				errx(EXIT_FAILURE, _("invalid argument to --auto/-a option"));
> +			ctl->a_arg = parse_switch(optarg, "on", "off");

I have extended parse_switch() to accept arbitrary number of 1|0 pairs
and also error message, so:

  ctl->a_arg = parse_switch(optarg, _("argument error"),
                        "on", "off",  "1", "0",  NULL);


Idea: maybe we can remove all the _("argument error") strings from all
strtoxxx_ functions and use some global function to generate proper user 
friendly error messages from longopts[], something like:


while ((c = getopt_long(argc, argv, "a:b:", longopts, NULL)) != -1) {

    err_strtoxxx_set_options(c, longopts);       <--- !!!

    switch (c) {
    case 'a':
        foo = strtol_or_err(optarg);
        break;
    case 'b':
        bar = parse_switch(optarg, "on", "off", NULL);
        break;
    }
}


where err_strtoxxx_set_options() sets in strutils.c static

        ul_cur_option = c;
        ul_lonfopts = longopts;

then on error strtol_or_err() and parse_switch() will use the ul_
variables to print

     "invalid argument to --auto/-a: %s", optarg

and if ul_* are not initialized then something generic ("argument
error"). The result will be less code in main(), better error messages,
and code independent on the names of the options.

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux