Program parameters in Linux

Source: Internet
Author: User
Tags coding standards
Linux program parameters-general Linux technology-Linux programming and kernel information. The following is a detailed description. The command parameters in Linux can be in the following two forms:

(1) Short parameters: one connection number and one lower-case letter (or upper-case letters, but lower-case letters are more convenient for user input)

(2) Two Connection Numbers of the long parameter and a lowercase string

The results of the two methods are exactly the same. The only difference is that the "short format" is used for user input, and the "long format" is used for clearer display. For example, the use of-h and -- help

GNU Coding Standards list the names of some commonly used command-line options. if you plan to provide any options similar to these, it's a good idea to use the names specified in the coding standards. this makes it easier for users to use. you can use the following command to view GNU/Linux Coding Standards

% Info "(standards) User Interfaces"

Parse command parameters in C/C ++: You can use the getopt_long library provided by GNU to parse Long and Short commands. You can.

To use getopt_long, you must define two data structures. the first character is a string listing all possible short commands. Each command contains one character. If an extra parameter is required for a mission, a colon is followed by the character, for example, if the valid parameters of your program include-h,-o filename, and-v, you can use the following string to represent ho: v

The second data structure is a long parameter, which must be a struct option array. Each element of this array has four fields, the first is the long parameter name (a string without two Connection Numbers), the second field is a value, 1 indicates that additional parameters are required, and 0 indicates that no additional parameters are required. the third field must be NULL, and the fourth field is a character that identifies the short parameter name corresponding to this long command .. and the four fields of the last element of the array must be empty. for example:

Const struct option long_options [] = {
{"Help", 0, NULL, 'H '},
{"Output", 1, NULL, 'O '},
{"Verbose", 0, NULL, 'V '},
{NULL, 0, NULL, 0 },
};

When processing parameters that require additional parameters, you can use the global variable optarg, which is a string containing sub-parameter characters.
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.