This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Getting Argument Name as Passed to an argp Parser?


From inside an argp parser, is there any way to get the name of the argument being processed, as it was passed on the command line, rather than just its key?

(The canonical form would be fine, as opposed to the exact text as recognized through unique prefix.)

E.g. given an argp_option element

{ "foo", 'f', "VALUE", 0, "Use VALUE for foo;\nVALUE is `a' or `b'\n(default: `a')", 0 }

and a parse_opt including the fragment

  arguments *arguments = state->input;
  switch (key) {
    case 'f':
      switch (*arg) {
        case 'a': arguments->foo = A; break;
        case 'b': arguments->foo = B; break;
        default: argp_failure(state, 1, EINVAL, "foo `%s'", arg);
      }
      break;

I'd like to be able to make the error message automatically include the argument as called (i.e. either "-f" or "--foo").

E.g. if I had "default: argp_failure(state, 1, EINVAL, "--foo `%s'", arg);" I'd get the result I'm looking for for "--foo c" or "--foo=c".

(I tried "default: argp_failure(state, 1, EINVAL, "%s `%s'", state->argv[state->next-2], arg);", but that only works for "-f c" and "--foo c", not "-fc" or "--foo=c".)

A couple ways this might be implemented:

An argp_state field providing the (possibly canonicalized) name of the option as called from the command-line would be ideal, as I could then say "default: argp_failure(state, 1, EINVAL, "%s `%s'", state->called_as, arg);".

Alternatively, this could be three state fields: a pointer to the argp I passed in, an offset into argp.options, and a flag indicating whether the option was recognized by its short name:

        default:
	  if(state->by_short_name)
	    argp_failure(state, 1, EINVAL, "-%c `%s'", state->argp->options[state->option].key, arg);
	  else
	    argp_failure(state, 1, EINVAL, "--%s `%s'", state->argp->options[state->option].name, arg);

The end result in either case would be

$ cmd -fc
cmd: -f `c': Invalid argument
$ cmd -f c
cmd: -f `c': Invalid argument
$ cmd --fo c
cmd: --foo `c': Invalid argument
$ cmd --fo=c
cmd: --foo `c': Invalid argument
$ cmd --foo c
cmd: --foo `c': Invalid argument
$ cmd --foo=c
cmd: --foo `c': Invalid argument

Is there a way to do this with the current argp package that I've missed, or is this a feature request?
-- 
Aaron Davies
aaron.davies@gmail.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]