I recently wrote something about the LS command function, but I am a newbie. Currently, only-L,-a,-al. is developing towards-R.
At the time of writing, I heard the getopt function mentioned by Huan ge. I made a little research. It is quite convenient to process parameters.
For example, if your function has parameters in multiple cases, do you have to use the strcmp function to determine whether it is more convenient to use getopt. Maybe it is not difficult to implement-l-A and the combination of-Al. However, if you want to implement multiple situations, it will be very painful. For example, if you want to implement the-l-a-r-s-I parameters, if you do not consider the combination, there will be only five (but only five judgments ), when considering the combination, there are 10 two combinations, 10 for three, 5 for four, and 1 for five. In these cases, the number of statements to be written is implemented. If you call the getopt function, you can skip this step to list the functions to be implemented in various cases, next we will introduce this function.
The header file is in # inlcude <unistd. h>
Function Syntax: int getopt (INT argc, char * const argv [], const char * optstring );
The first two parameters are the two parameters in int main (INT argc, char ** argv; the third parameter is the option you want to pass the items in argv.
The third parameter optstring of getopt has several requirements.
1. Single Character: indicates the option
2. A single character followed by a colon: indicates that this option must be followed by a parameter. Parameters are immediately followed by options or separated by spaces.
3. A single character is followed by two colons, indicating that this option must be followed by a parameter. Parameters must be followed by no space.
When the getopt function is called, its successful call is an option. -1 is returned if the corresponding options are not found in the command parameters. When processing a parameter with '-', the character followed by '-' is the option name.
And then the parameter. By the way, getopt has several macros. The usage and definition of these macros are described below.
Extern char * optarg: it is the parameter pointer of the command parameter (excluding '-_') option, but if we do not enter a parameter for its options, it is a null pointer.
Extern int optind: each time we call getopt, optind moves. When getopt is called next time, check the option again from the position where optind is stored.
Extern int opterr: The variable used to output errors. When opterr is set to 0, getopt does not output error messages to stderr.
Extern int optopt: When the command line option character is not included in optstring or the option lacks necessary parameters, this option is stored in optopt, and getopt returns '? ',
The test code in the previous section is as follows:
# Include <unistd. h> # include <stdio. h> # include <stdlib. h> int main (INT argc, char * argv []) {int K; opterr = 0; If (k = getopt (argc, argv, "A: B :: C ") {Switch (k) {Case 'A': printf (" the optopt: % C, optarg = % s \ n ", optopt, optarg); break; case 'B': printf ("the B optopt: % C, optarg = % s \ n", optopt, optarg); break; Case 'C ': printf ("the C optopt: % C, optarg = % s \ n", optopt, optarg); break; case '? ': Printf ("invalid parameter input optopt: % C, optarg = % s \ n", optopt, optarg); break ;}} return exit_success ;}
GCC Compilation: GCC test. C-o Test
Run:./test-A. The result is: "The input optopt: A, optarg = (null) does not conform to the specified parameter )"
Run:./test-a hello. The output result is: "The A optopt:, optarg = Hello"
Run:./test-C. The output result is: "The C optopt:, optarg = (null )"
The output result is the same as expected.
We recommend that you use this function to process external input parameters, which is convenient and efficient.