Getopt (parsing command-line arguments)

Source: Internet
Author: User

ref:http://vopit.blog.51cto.com/2400931/440453 related function header file#include <unistd.h>
Defining Functionsint getopt (int argc,char * CONST argv[],const char * optstring);
function DescriptionGetopt () is used to parse command-line arguments. The parameters argc and argv are the number and contents of the arguments passed by main (). The parameter optstring represents the option string to be processed. This function returns the next option letter in argv, which corresponds to the letter in the parameter optstring. If the letter in the option string is followed by the colon ":", then there are related arguments, and the global variable, OPTARG, points to this extra parameter. If getopt () does not find a matching parameter, it prints an error message and sets the global variable optopt to "?". Character, if you do not want getopt () to print the error message, simply set the global variable Opterr to 0. definition of short parameters        Getopt () uses a string referred to by optstring as a short argument list, like "1ac:d::" is a short argument list. The definition of a short parameter is a '-' followed by a letter or number, like-A,-B is a short argument.   Each number or letter defines a parameter. The short parameters in the getopt definition are divided into three kinds: 1.  A parameter without a value, which is defined as the parameter itself.  2. Must have a value parameter, which is defined by adding a colon after the parameter itself.  3. An optional value parameter, which is defined by adding two colons after the parameter itself.  Take the above "1AC:D:" as a sample to illustrate, where the 1,a is a parameter without value, C is a parameter that must take a value, D is an optional value of the parameter. In the actual call, ' -1-a-C cvalue-d ', ' -1-a-C cvalue-ddvalue ', ' -1a-ddvalue-c cvalue ' are all legal. Here are three points to note: 1.  Parameters without values can be ligatures, such as 1 and a are parameters without values, they can be -1-a separately, or they can be -1a or-a1 ligatures.  2. The parameters are not in order, ' -1a-c cvalue-ddvalue ' and '-d-c cvalue-a1 ' parse result is the same. 3. Note that there can be no space between the value of an optional value parameter and the parameter, which must be written in a format such as-ddvalue, which resolves the error if written in the format of-D dvalue.
return valueGetopt () Each call returns the arguments passed in at a command line at a time.    Getopt () returns 1 when there is no last call to the parameter.   When parsing to a parameter that is not inside the optstring, or if a required value parameter does not have a value, return '? '. When Optstring starts with ': ', it returns ': ' Instead of '? ' in the case of a missing value parameter.
#include <stdio.h>#include<unistd.h>intMainintargcint*argv[]) {        intch; Opterr=0;  while(ch = getopt (argc, (Char*Const*) argv,"A:B::CDE"))!=-1)        {                Switch(CH) { Case 'a': printf ("option A: '%s ' \ n", Optarg);  Break;  Case 'b': printf ("option b:b\n");  Break; default: printf ("Other option:%c\n", CH); }} printf ("optopt +%c\n", optopt);}

Getopt (parsing command-line arguments)

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.