Getopt_long () function _c language for parsing the command line in detail

Source: Internet
Author: User

Header file
#include <getopt.h>

Function prototypes
int getopt_long (int argc,char * Const argv[],const char *optstring,const struct option *longopts,int *longindex)

Function description
Getopt is used to parse command-line option arguments.
Getopt_long supports command-line parsing of long options, and the parameters argc and argv in a function are usually passed directly from two parameters of main (). Optstring is a string of option parameters.

The string optstring can be the following elements:
1. Single character, representing options,

2. A single character followed by a colon: this option must be followed by a parameter. Parameters are immediately followed by an option or separated by a space. The pointer to the parameter is assigned to Optarg.

3. A single character followed by a two colon, indicating that the option can have parameters or no parameters. If there are parameters, the arguments must be immediately after the option and cannot be separated by a space. The pointer to the parameter is assigned to Optarg. (This feature is the GNU expansion).
Optstring is a string that represents acceptable parameters. For example, "A:B:CD", which indicates that an acceptable parameter is a,b,c,d, where a and B parameters are followed by more parameter values. (for example,-a host--b name).

parameter longopts is actually an instance of a struct:

Copy Code code as follows:

struct Option {
const char *name; Name indicates the name of the long parameter
int Has_arg//has_arg has 3 values, no_argument (or 0), indicating that the parameter is not followed by the parameter value
Required_argument (or 1) indicates that the parameter must be followed by a parameter value
Optional_argument (or 2) indicates that the parameter can be followed or not with the parameter value
int *flag;
Used to determine what the return value of the Getopt_long () is. If the flag is NULL, the function returns the Val value that matches the item option
int Val; and flag joint decision return value
}

Give an example:
Copy Code code as follows:

struct option long_options[] = {
{"A123", required_argument, 0, ' a '},
{"C123", no_argument, 0, ' C '},
}

Now, if the command line argument is-a 123, then calling Getopt_long () returns the character ' a ' and returns the string 123 from Optarg (Note!). String 123 brought back by Optarg! Optarg does not need to be defined and is already defined in getopt.h), then if the command line argument is-C, then the call to Getopt_long () returns the character ' C ', at which point optarg is null. Finally, when getopt_long () resolves all of the command line arguments, returns-1.

Parameter longopts is actually an instance of a struct:

Copy Code code as follows:

struct Option {
const char *name; Name indicates the name of the long parameter
int Has_arg//has_arg has 3 values, no_argument (or 0), indicating that the parameter is not followed by the parameter value
Required_argument (or 1) indicates that the parameter must be followed by a parameter value
Optional_argument (or 2) indicates that the parameter can be followed or not with the parameter value
int *flag;
Used to determine what the return value of the Getopt_long () is. If the flag is NULL, the function returns the Val value that matches the item option
int Val; and flag joint decision return value
}

Give an example:
Copy Code code as follows:

struct option long_options[] = {
{"A123", required_argument, 0, ' a '},
{"C123", no_argument, 0, ' C '},
}

Now, if the command line argument is-a 123, then calling Getopt_long () returns the character ' a ' and returns the string 123 from Optarg (Note!). String 123 brought back by Optarg! Optarg does not need to be defined and is already defined in getopt.h), then if the command line argument is-C, then the call to Getopt_long () returns the character ' C ', at which point optarg is null. Finally, when getopt_long () resolves all of the command line arguments, returns-1.
Example
Copy Code code as follows:

#include <stdio.h>
#include <getopt.h>
Char *l_opt_arg;
char* Const Short_options = "NBL:";
struct option long_options[] = {
{"Name", 0, NULL, ' n '},
{"Bf_name", 0, NULL, ' B '},
{"Love", 1, NULL, ' l '},
{0, 0, 0, 0},
};
int main (int argc, char *argv[])
{
int C;
while ((c = Getopt_long (argc, argv, Short_options, Long_options, NULL))!=-1)
{
Switch (c)
{
Case ' n ':
printf ("My name is xl./n");
Break
Case ' B ':
printf ("His name is st./n");
Break
Case ' l ':
L_opt_arg = Optarg;
printf ("Our Love Is%s!/n", l_opt_arg);
Break
}
}
return 0;
}
[Root@localhost wyp]# gcc-o getopt getopt.c
[Root@localhost wyp]#./getopt-n-b-l Forever
My name is XL.
His name is ST.
Our Love is forever!
[Root@localhost liuxltest]#
[Root@localhost liuxltest]#./getopt-nb-l Forever
My name is XL.
His name is ST.
Our Love is forever!
[Root@localhost liuxltest]#./GETOPT-NBL Forever
My name is XL.
His name is ST.
Our Love is forever!

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.