All of lore.kernel.org
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Sami Kerola <kerolasa@iki.fi>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH 09/16] eject: use parse_switch()
Date: Tue, 24 Feb 2015 12:37:27 +0100	[thread overview]
Message-ID: <20150224113727.GK19430@ws.net.home> (raw)
In-Reply-To: <1424616106-580-10-git-send-email-kerolasa@iki.fi>

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@redhat.com>
 http://karelzak.blogspot.com

  reply	other threads:[~2015-02-24 11:37 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-22 14:41 [PATCH 00/16] pull: changes to various utils Sami Kerola
2015-02-22 14:41 ` [PATCH 01/16] whereis: tell when mandatory option is missing Sami Kerola
2015-02-22 17:37   ` Benno Schulenberg
2015-02-24  9:40   ` Karel Zak
2015-03-01 11:08     ` Sami Kerola
2015-03-03 11:32       ` Karel Zak
2015-03-03 12:59         ` Sami Kerola
2015-03-03 23:07           ` Sami Kerola
2015-02-22 14:41 ` [PATCH 02/16] flock: add --verbose option Sami Kerola
2015-02-22 17:44   ` Benno Schulenberg
2015-03-01 11:11     ` Sami Kerola
2015-02-22 14:41 ` [PATCH 03/16] flock: improve timeout handling Sami Kerola
2015-02-24 10:21   ` Karel Zak
2015-03-01 11:46     ` Sami Kerola
2015-03-02  9:13       ` Karel Zak
2015-03-05  9:36   ` Karel Zak
2015-02-22 14:41 ` [PATCH 04/16] prlimit: tell in --verbose output which pid got the new limit Sami Kerola
2015-02-24 10:25   ` Karel Zak
2015-02-22 14:41 ` [PATCH 05/16] tunelp: remove get_val() in favour of strtol_or_err() Sami Kerola
2015-02-24 10:29   ` Karel Zak
2015-02-22 14:41 ` [PATCH 06/16] tunelp: remove unnecessary preprocessor directives Sami Kerola
2015-02-22 14:41 ` [PATCH 07/16] lib/strutils: move parse_switch() from setterm(1) to library Sami Kerola
2015-02-22 17:12   ` Benno Schulenberg
2015-02-24 10:34   ` Karel Zak
2015-02-22 14:41 ` [PATCH 08/16] tunelp: use parse_switch() Sami Kerola
2015-02-24 10:40   ` Karel Zak
2015-02-22 14:41 ` [PATCH 09/16] eject: " Sami Kerola
2015-02-24 11:37   ` Karel Zak [this message]
2015-03-01 11:49     ` Sami Kerola
2015-02-22 14:41 ` [PATCH 10/16] rpmatch: use symbolic value when evaluation return codes Sami Kerola
2015-02-24 11:47   ` Karel Zak
2015-02-22 14:41 ` [PATCH 11/16] tailf: check printing criteria more carefully Sami Kerola
2015-02-24 11:53   ` Karel Zak
2015-03-01 11:55     ` Sami Kerola
2015-02-22 14:41 ` [PATCH 12/16] tailf: count last lines correctly at initial print out Sami Kerola
2015-02-24 12:12   ` Karel Zak
2015-03-01 12:09     ` Sami Kerola
2015-02-22 14:41 ` [PATCH 13/16] tailf: do not allow minus signed last lines argument Sami Kerola
2015-02-22 14:41 ` [PATCH 14/16] tailf: ensure file argument really is a file Sami Kerola
2015-02-24 12:18   ` Karel Zak
2015-03-01 12:20     ` Sami Kerola
2015-02-22 14:41 ` [PATCH 15/16] logger: move /dev/log to pathnames.h Sami Kerola
2015-02-24 12:19   ` Karel Zak
2015-02-22 14:41 ` [PATCH 16/16] logger: fix -i argument parsing regression Sami Kerola
2015-02-22 17:21   ` Benno Schulenberg
2015-03-01 11:06     ` Sami Kerola
2015-02-22 17:10 ` [PATCH 11/62] pull: Benno Schulenberg
2015-03-05 10:16 ` [PATCH 00/16] pull: changes to various utils Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150224113727.GK19430@ws.net.home \
    --to=kzak@redhat.com \
    --cc=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.