Getopt (3) -- Translation from Linux programmer's Manual

Source: Internet
Author: User

# Include <unistd. h>

 

Int getopt (INT argc, char * const argv [], const char * optstring );

Extern char * optarg;

Extern int optind, opterr, optopt;

 

Description:

The getopt () function parses the command line parameters. The first two parameters -- argc and argv are the same as the parameters passed in when the main function is started. The elements starting with "-" or "--" (not necessarily the two) in array argv are called option elements ), the string except "-" or "--" in the option element becomes the option character string (option characters ). If getopt () is called repeatedly, it returns each option character in each option element.

 

The optind variable is the subscript of the elements to be processed in the next argv array. The system initializes this value to 1. the caller can re-scan the argv by restoring it to 1 or scanning a new parameter array.

 

If getopt () finds another option character, it returns this character and updates optind and nextchar, so that the next scan can know whether to read one from the string or the next argv element.

 

If no more option strings exist, getopt () returns-1, and optind becomes the first non-option element subscript in argv.

 

Optstring is a string that contains valid option characters. If a character is followed by a colon, this option requires a parameter, so getopt () puts a pointer into optarg, which may point to the following string in the same argv element, or the next argv element, because some parameters are directly followed by the option, such as-lmemcached, which indicates loading the file named libmemcached. so shared library. Otherwise, if the character in optstring is not followed by a colon, The optarg is set to 0.

 

By default, getopt () changes the scanning sequence of argv so that all non-options are finally scanned.

 

If getopt () scans unknown option characters, print the error message to stderr, save the character to optopt, and return '? ', The caller can prevent error information by setting opterr to 0.

 

Note that the first letter in optstring is the GNU extension of "+...

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.