Turn off abbreviation in getopt_long (getopt.h)?
Asked Answered
H

3

6

Is it possible to turn off abbreviation in getopt_long()? From the man page:

Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option.

I want to do this because the specification I have received for a piece of code requires a full-length exact match of the flags, and there are many flags.

Haug answered 3/3, 2011 at 14:29 Comment(0)
S
4

It appears there isn't a way to disable the abbreviation feature. You aren't alone in wishing for this feature. See: http://sourceware.org/bugzilla/show_bug.cgi?id=6863

Unfortunately, It seems the glibc developers don't want the option as the bug report linked above was resolved with "WONTFIX". You may be out of luck here :-\

Swag answered 3/3, 2011 at 15:41 Comment(0)
M
2

If you use argp_parse() instead of getopt() (highly reccommended, BTW), you can access the exact flag entered by the user through

state->argv[ state->next - 2 ]

It's a bit of a hack, but should work.

Malanie answered 3/3, 2011 at 19:22 Comment(0)
S
2

This is not perfect solution but you can check exact arg given by a user after calling getopt_long() (normally within switch) like below:

if (strcmp(argv[optind-1], "--longoption") == 0)

optind points a next argument that you need to process. Thus, you can access the original arg using optind-1.

Shawnshawna answered 26/10, 2016 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.