Linux getopt_long ()

Source: Internet
Author: User

File

# Include <getopt. h> Function prototype 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 parameters. Getopt_long supports command line parsing of Long Options. Use man getopt_long. The declaration is as follows: int getopt_long (INT argc, char * const argv [], const char * optstring, const struct option * longopts, int * longindex); argc and argv in the function are usually passed directly from the two parameters of main. Optsting is a string composed of option parameters. The optstring can contain the following elements: 1. A single character, indicating the option; 2. A single character followed by a colon: indicating that the option must be followed by a parameter. Parameters are immediately followed by options or separated by spaces. The pointer of this parameter is assigned to optarg. 3. A single character is followed by two colons, indicating that this option can be followed by a parameter or no parameter. If a parameter exists, the parameter must follow the option and cannot be separated by spaces. The pointer of this parameter is assigned to optarg. (This feature is the expansion of GNU ). Optstring is a string that indicates acceptable parameters. For example, "A: B: cd" indicates that the acceptable parameters are A, B, C, and D. Among them, parameters A and B are followed by more parameter values. (For example,-a host-B Name) The longopts parameter is actually a structured instance: struct option {const char * Name; // name indicates the long parameter name int has_arg; // has_arg has three values, no_argument (or 0), indicating that this 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), indicating that the parameter can be followed or not determined by the parameter value int * flag, what is the returned value of getopt_long. If the flag is null, the function returns the int Val value that matches the option. // the return value is determined by the combination of the flag and the flag.} an example is provided: struct option long_options [] = {"A123", required_argument, 0, 'A'}, {"c123", no_argument, 0, 'C'},} Now, if the argument of the command line is-a 123, calling getopt_long () will return the 'A' character, and the string 123 will be returned by optarg (note! String 123 is taken back by optarg! Optarg does not need to be defined. In getopt. if the command line parameter is-C, calling getopt_long () will return the 'C' character. In this case, optarg is null. Finally, when getopt_long () resolves all parameters in the command line, return-1. Example # 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 },}; 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. c [root @ localhost wyp] #. /getopt-n-B-l forevermy name is XL. his name is St. our love is forever! [Root @ localhost liuxltest] # [root @ localhost liuxltest] #./getopt-Nb-l forevermy name is XL. His name is St. Our love is forever! [Root @ localhost liuxltest] #./getopt-NBL forevermy 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.