How to specify an optstring in the getopt function?
The getopt(3)
manpage makes it pretty clear :
- the string itself is used for specifying the legal options that can appear on the commandline,
- if the option is followed by a
:
, then that option has a required parameter - not specifying it will cause the function to fail, - if the option is followed by a
::
, 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.
If colon :
is followed by a char or string means this option must require the argument
and if there are no colon means no arguments
for more details do man 3 getopt
or visit the link or manpage
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
.