Ramya Ramya - 2 months ago 6x
C Question

C getopt -<integer>

How do I get the option -10 from command line arguments- "tail -10". getopt function finds '1' character. But how do I access the string "10"?

If this can be done by getopt_long, an example would help. Thanks.

R.. R..

Unless you intend for -1 to be an option with 0 as its argument, the answer is you don't. getopt is only made for processing options that fit the standard POSIX utilities' option syntax. It may be possible to use GNU getopt_long for this purpose, or you could just write your own argv parser (it's easy).

Edit: Actually I think I misread what you want. If you want - followed by any number to be interpreted as an option with that numeric value, I don't think there's any version of getopt that will work. There's no way you can special-case every single number as an option, and if you simply tell getopt that all of the digits are option characters that take arguments, -123 will be interpreted as a -1 option with an argument of 23 (which is fine, you can interpret it from there), but a lone -1 will cause the next argv element to get eaten as an argument to -1, which is difficult or impossible to recover from.