Use getopt to get command line parameters in Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. Function Description
Header file: # include
Function declaration: int getopt (int argc, char * const argv [], const char * optstring );
Function Description: getopt () is used to analyze command line parameters. The argc and argv parameters are the number and content of parameters passed by main. The optstring parameter indicates the option string to be processed. This function returns the option letter in argv, which corresponds to the letter in the optstring parameter. If the letter in the option string is followed by the colon ":", it indicates that there are related parameters. The global variable optarg points to this additional parameter. If getopt () fails to find the correct parameter, an error message is printed and the global variable optopt is set to "?". If you do not want getopt () to print the error message, you only need to set the global opterr to 0.
The usage and definition rules of the options are similar:
AB: c ::
Meaning:
A is not followed by a colon, indicating that no parameter is allowed.
B is followed by a colon, indicating that there is a necessary parameter
C is followed by two colons, indicating that there is an optional parameter
Long options have the same definitions, but are separated by commas.
Return Value: If a matching parameter is found, a letter is returned. If the parameter is not included in the option Letter of The optstring parameter, "?" is returned. Character.-1 is returned when the analysis ends.
2. Example Program
The following is an example program that uses the getopt function to read parameters. Is this program supported? P? S? B? C parameters, and read and print these parameter values. You can use the code for your own program as needed.