[C Language] getopt parameter optind and its main (INT argc, char ** argv) Parameter Interpretation

Source: Internet
Author: User

Getopt is used to parse command line option parameters.
# Include <unistd. h>

Extern char * optarg; // parameter pointer of the option
Extern int optind, // when getopt is called next time, check the option again from the position where optind is stored.
Extern int opterr, // when opterr = 0, getopt does not output an error message 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 '? '

 

 1 #include <stdio.h>   2 #include <unistd.h>   3 #include <stdlib.h> 4  5 #ifdef RDP2 6 #define VNCOPT "V:Q" 7 #else 8 #define VNCOPT "V:" 9 #endif10  11 int main(int argc, char **argv)  12 {  13         int ch, i;  14         opterr = 0;15 16         while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-1)  17         {  18             printf("optind:%d,opterr:%d\n", optind, opterr);  19                 switch(ch)  20                 {         21                         case ‘V‘:22                                 printf("option V:‘%s‘\n",optarg);23                                 break; 24                         case ‘a‘: 25                                 i = strtol(optarg, NULL, 16);26                                 printf("i = %x\n", i); 27                                 printf("option a:‘%s‘\n",optarg);  28                                 break;  29                         case ‘b‘:  30                                 printf("option b :b\n");  31                                 break; 32                         case ‘e‘:33                                 printf("option e:‘%s‘\n",optarg);34                                 break; 35                         default:  36                                 printf("other option :%c\n",ch);  37                 }  38         }39         for(int i = 0; i < argc; i++)40             printf("argv[%d] = [%s]\n", i, argv[i]);41         printf("argc = [%d]\n", argc); 42         printf("optopt +%c\n",optopt);  43 44     return 1;45 }

Execution result:

$ ./getopt -a 123 -bcd -e 456i = 123option a:‘123‘optind:3,opterr:0option b :boptind:3,opterr:0other option :coptind:3,opterr:0other option :doptind:4,opterr:0option e:‘456‘optind:6,opterr:0argv[0] = [./getopt]argv[1] = [-a]argv[2] = [123]argv[3] = [-bcd]argv[4] = [-e]argv[5] = [456]argc = [6]optopt +eoptind = 6

 

Argv array stores command line strings (including./getopt executable programs)

Argc command line string count (./getopt-A 123-BCD-e 456) 6

When optind calls getopt next time, it re-checks the option from the position where optind is stored. Start from 0

(Corresponding to the following position)

./Getopt-A 123-BCD-e 456

0 1 2 3 4 5 6

 

 

Related Article

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.