Usage of the "getopt_long" command line option for linux Parsing

Source: Internet
Author: User

The getopt_long command line option for linux Parsing is required in the program. You can select the option for parsing the command line by yourself, but there is a ready-made option. Why bother re-engineering the wheel.
The following describes how to use getopt_long to parse the command line.
The program mainly uses: www.2cto.com whether the short option long option requires parameter remarks-v -- version no query version number-n -- name is (user name) specify whether the user-d -- debug has been tested. 1. function source [cpp] # include <getopt. h> // getopt_long () header file location int getopt_long (int ___ argc, char * const * ___ argv, const char * _ shortopts, const struct option * _ longopts, int * _ longind); int getopt_long_only (int ___ argc, char * const * ___ argv, const char * _ shortopts, const struct option * _ longopts, int * _ longind );
2. argc argv: The shortopts: short option string passed directly from the main function. For example, "n: v", it should be noted that the short option string does not require '-', and when the option needs to pass a parameter, add ":" After the short option. Longopts: struct option array, used to store long option parameters. Longind: returns the index value of the long option in the longopts struct array for debugging. If it is set to NULL, the following describes struct option [cpp] struct option {const char * name; // long option name int has_arg; // whether the parameter int * flag is required; int val ;}; www.2cto.com name: Long option name has_arg: whether a parameter is required. There are three values: [cpp] # define no_argument 0 // No parameter is required # define required_argument 1 // The parameter # define optional_argument 2 // The parameter flag and valflag and val are mutually dependent, there are two main cases: (1) if the flag is NULL and the val value is used to determine the long option, you must specify a unique val value for the long option. This also serves as a bridge between long and short options. (2) If the flag is not NULL, the val value is stored in the bucket pointed to by the flag to identify that the long option has appeared. 3. If the short option is used in the return value program, the short option character (such as 'n') is returned. If the required parameter is yes, the parameter is saved to the optarg before the return. The length option is used in the program. The return value is determined by flag and val. If the flag is NULL, the val value is returned. So different processing is performed based on the val value, which also indicates that the val must be unique. If the val value is equal to the short option value, you can use the short option to parse the function long option. If the flag is not NULL, the val value is saved to the bucket pointed to by the flag, getopt_long returns 0 with undefined long or short options. getopt_long returns? After parsing, getopt_long returns-1
4. The instance theory should be combined with practice [cpp] # include <stdio. h> # include <stdlib. h> # include <getopt. h> // getopt_long () header file location www.2cto.com int main (int argc, char ** argv) {const char * optstring = "n: v"; int c, deb, index; struct option opts [] = {"username", required_argument, NULL, 'n'}, {"version", no_argument, NULL, 'V'}, {"debug ", no_argument, & deb, 1}, {0, 0, 0}; while (c = getopt_long (argc, argv, optstring, opts, & index ))! =-1) {switch (c) {case 'N': //-n or -- username specifies the username printf ("username is % s \ n", optarg); break; case 'V': //-v or -- version. The output version is printf ("version is 0.0.1 \ n"); break; case 0: // flag is not NULL printf ("debug is % d \ n", deb); break; case '? ': // The printf ("? \ N "); break; default: printf (" c is % d \ n ", c); break ;}www.2cto.com return 0;} run:
Description: getopt_long_only. This function is the same as getopt_long. It can only be followed by a long option name, such as./main-username jackie author lanyan822.

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.