C and pointer notes-getopt Function

Source: Internet
Author: User

This article based on http://jesserei.blog.163.com/blog/static/121411689200983081421390/, slightly modified

Generally, various commands in Linux have many command line parameters to choose from, such:
Gcc-g-LM Foo. C-o foo
Getopt () is a function used to analyze command line parameters.
Before continuing to discuss getopt, we must clarify two concepts: option and argument ).
In the above example, G in-G, O in-O and L in-LM are options, and m in-LM is the option parameter of L, -Foo in O foo is the option parameter of O. Therefore, we know that there are two options: one with the option parameter and the other without the option parameter.
Next we can continue to discuss getopt.

First, the function declaration:
# Include <unistd. h>
Extern char * optarg;
Extern int optind;
Extern int optopt;
Extern int opterr;
Extern int optreset;
Int getopt (INT argc, char * const * argv, const char * optstring );

Getopt () returns an option each time it is called.
Argc and argv are obviously two parameters of the main function.
The character string optstring can contain the following elements: a single character followed by a colon, followed by an option parameter, followed by two colons, followed by a dispensable option parameter. For example, an option character "X" indicates the option "-X", an option character "X:" indicates the option and its parameter "-x argument", and an option character "X :: "The parameter of option X is optional (Note that there cannot be spaces between the value of the parameter and the parameter of the optional value. It must be written in the format of-ddvalue, if it is written in a format like-D dvalue, it will parse the error) (":" is added by GNU and may not be used in all UNIX systems ).
After getopt () is returned, if there is an option parameter, optarg points to the option parameter, and the variable optind contains the next argv parameter as the index for the next call of getopt. The optopt variable saves the last known option returned by getopt.
When the argument series has reached the end, the getopt () function returns-1. When an unknown option is encountered, getopt returns '? '. The option interpretation in the parameter column may be canceled by '--' because it causes getopt () to send an end signal to the parameter processing and return-1.
For example:
./Getopt-a ---C-B foo
The source code of getopt is below. getopt will only be interpreted as-.

The following is a simple example:

#include <stdio.h>#include <unistd.h>int main(int argc, char * argv[]){        int aflag=0, bflag=0, cflag=0;        int ch;        // opterr = 0;        while ((ch = getopt(argc, argv, "ab:cd::")) != -1)        {                printf("optind: %d\n", optind);                switch (ch) {                        case 'a':                        printf("HAVE option: -a\n");                        aflag = 1;                        break;                        case 'b':                        printf("HAVE option: -b\n");                        bflag = 1;                        printf("The argument of -b is %s\n", optarg);                        break;                        case 'c':                        printf("HAVE option: -c\n");                        cflag = 1;                        break;                        case 'd':                        printf("HAVE option: -d\n");                        printf("The argument of -d is %s\n", optarg);                        cflag = 1;                        break;                                case '?':                        printf("Unknown option: %c\n",(char)optopt);                        break;                }        }}

The program is very simple. I will not explain it here.

Both the opterr and optind variables are initialized to 1. If you want to skip the first few parameters of the command line, you can set optind to another value before calling getopt.
If you do not want getopt () to output error information, set the global variable opterr to 0.

In GNU Linux, getopt is extended to provide support for long parameters, such as "-- help. It mainly provides the getopt_long function, and I have not yet fully learned it. I will introduce it in a short article after I have learned it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.