Getopt, getoptlong learning, getoptgetoptlong

Source: Internet
Author: User

Getopt, getoptlong learning, getoptgetoptlong
Getopt and getoptlong are used to parse command line parameters. 1. getopt

#include <unistd.h>extern char *optarg;  extern int optind,   extern int opterr,  extern int optopt;  int getopt(int argc, char * const argv[], const char *optstring);

Four global variables are defined: optarg is the parameter pointer of the option. optind records the current location. When opterr is set to 0, getopt does not output error information to stderr. When the Command Option character is not included in optstring or the required parameter is missing, this option is stored in optopt, and getopt returns '? '

Getopt is called once, and an option is returned. If no parameter is found,-1 is returned. Meanwhile, optind stores the first command line parameter that does not contain the option. The optstring parameter can have the following elements. 1. single Character: Option 2. A single character followed by a colon indicates that the option must be followed by a parameter. The parameter is immediately followed by the option, or is separated by a space. The parameter pointer is assigned to optarg. 3. A single character followed by two colons (:) indicates that this option must be a parameter and cannot be separated by spaces. Example:
#include <stdio.h>#include <stdlib.h>#include <unistd.h> int main(int argc, char **argv){    int opt = 0;     while ((opt = getopt(argc, argv, "if:?lr::")) != -1)    {        switch(opt)        {            case 'i':            case 'l':                printf("option: %c\n", opt);                break;            case 'f':                printf("filename: %s\n", optarg);                break;            case 'r':                printf("arg:%s\n", optarg);                break;            case ':':                printf("option needs a value\n");                break;            case '?':                printf("unknow option: %c\n", optopt);                break;        }    }      for (; optind < argc; optind++)    {        printf("argument: %s\n", argv[optind]);     ar     return 0;}
2. getoptlong is the getopt parameter supported by the long option. getoptlong has two more parameters than getopt. struct option * longopts and int * longindex are used less, generally, this parameter is set to NULL. Let's look at the struct option structure.
struct option{     char *name;     int has_arg;     int *flag;     int val;};
Name indicates the long parameter name, And has_arg has three values. When has_arg = no_argument (that is, 0), it indicates that this parameter is not followed by the parameter value, when has_arg = required_argument (1), it indicates that the parameter must be followed by the parameter value. When has_arg = optional_argument (2), it indicates that the parameter can be followed by the parameter value, or not. Flag when the pointer is null, the function directly returns the val value from the returned value of getopt_long. When it is not null, the val value is assigned to the integer indicated by the flag, and the return value of the function is 0. Val is used to specify the return value when the function finds this option, or when the flag is not empty, it specifies the value of the data pointed to by the flag. Example of defining option Scheme:
struct option longopts[] ={    {"initialize", 0, NULL, 'i'},    {"filename", 1, NULL, 'f'},    {"list", 0, NULL, 'l'},    {"restart", 0, NULL, 'r'}};
Example:
#include <stdio.h>#include <stdlib.h>#include <unistd.h> #define _GNU_SOURCE#include <getopt.h> int main(int argc, char **argv){    int opt;     struct option longopts[] =    {        {"initialize", 0, NULL, 'i'},        {"filename", 1, NULL, 'f'},        {"list", 0, NULL, 'l'},        {"restart", 0, NULL, 'r'}    };     while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1)    {        switch(opt)        {            case 'i':            case 'l':            case 'r':                printf("option: %c\n", opt);                break;            case 'f':                printf("filename: %s\n", optarg);                break;            case ':':                printf("option needs a value\n");                break;            case '?':                printf("unknow option: %c\n", optopt);                break;        }    }     for (; optind < argc; optind++)    {        printf("argument: %s\n", argv[optind]);     }     return 0;}

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.