Reputation: 1751
I'm not sure how to correctly use optstring
in the getopt
function in C.
How should that string be formatted? I saw examples where letters are next to each other, sometimes separated by a semicolon, sometimes by two semicolons.
What does it mean?
Upvotes: 29
Views: 32783
Reputation: 23709
It is just a string, and each character of this string represents an option. If this option requires an argument, you have to follow the option character by :
.
For example, "cdf:g"
accepts the options c
, d
, f
, and g
; f
requires an additional argument.
An option in command line looks like -option
, so you can use the options -c
, -d
, -f argument
and -g
.
Upvotes: 39
Reputation: 19286
The getopt(3)
manpage makes it pretty clear :
:
, then that option has a required parameter - not specifying it will cause the function to fail,::
, then that option has an optional parameter.The options are one-letter identifiers. For example, specifying a string like aB:cD::
as the optstring
will mean that your program takes options a
, B
with a required parameter, c
, and D
with an optional parameter.
Upvotes: 13