Use getopt to obtain command line parameters in Linux

Source: Internet
Author: User
Use getopt to get command line parameters in Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. Function Description
Header file: # include

Function declaration: int getopt (int argc, char * const argv [], const char * optstring );

Function Description: getopt () is used to analyze command line parameters. The argc and argv parameters are the number and content of parameters passed by main. The optstring parameter indicates the option string to be processed. This function returns the option letter in argv, which corresponds to the letter in the optstring parameter. If the letter in the option string is followed by the colon ":", it indicates that there are related parameters. The global variable optarg points to this additional parameter. If getopt () fails to find the correct parameter, an error message is printed and the global variable optopt is set to "?". If you do not want getopt () to print the error message, you only need to set the global opterr to 0.

The usage and definition rules of the options are similar:

AB: c ::

Meaning:
A is not followed by a colon, indicating that no parameter is allowed.
B is followed by a colon, indicating that there is a necessary parameter
C is followed by two colons, indicating that there is an optional parameter
Long options have the same definitions, but are separated by commas.

Return Value: If a matching parameter is found, a letter is returned. If the parameter is not included in the option Letter of The optstring parameter, "?" is returned. Character.-1 is returned when the analysis ends.

2. Example Program
The following is an example program that uses the getopt function to read parameters. Is this program supported? P? S? B? C parameters, and read and print these parameter values. You can use the code for your own program as needed.
QUOTE:
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.