Click to See Complete Forum and Search --> : using getopts


bsamuels
10-09-2003, 10:29 PM
I find the docs on this subject a bit confusing. Is it possible to use getopts to specify a variable integer as an option such as cmd -r -n filename, where n is an integer such as cmd -r -6 filename.

GaryJones32
10-10-2003, 04:23 AM
don't see why not '6' is just a char
but it's not in keeping with unix conventions..
what could be the long version
const struct option long_options[] = {
{"65978", 0,NULL,'6'}
};
aught to work like anything else ???
but like i said unix users are used to all command line and configure options following the same pattern. and 6 doesn't seem to stand for anything recognizable

bsamuels
10-10-2003, 09:08 AM
I meant that I want the integer to be an integer and signal an integer value. See the man pages for "head" where it is used in this way.

Stuka
10-10-2003, 09:35 AM
You can use it, but I think what you're looking for is the value setup for a flag, like so: tail -n 20 foo.txt If I'm on the right path, then yeah, it's easy - just put a colon after the flag spec in the string, like so "rn:v". When you do this, getopt() will give your switch the value in the optarg variable.

bwkaz
10-10-2003, 07:19 PM
Originally posted by Stuka
When you do this, getopt() will give your switch the value in the optarg variable. And then you use the atoi() function on optarg to convert it to an int.

optarg is a char *