Command Line Parameter Parsing function -- getopt ()

Source: Internet
Author: User

From: http://blog.csdn.net/mr_jj_lian/article/details/6835137

Command Line Parameter Parsing function -- getopt ()

The getopt () function declaration is as follows:
# Include <unistd. h>

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

Extern char * optarg;
Extern int optind, opterr, optopt;
The argc and argv parameters of this function are usually directly transmitted from the main () parameters. Optstring is a string consisting of option letters. If any character in the string is followed by a colon, this option requires an option parameter. When the number of getopt () command parameters is specified ( argc), The array pointing to these parameters ( argv) And the option string ( optstring, getopt()The first option is returned and some global variables are set. When you call this function again with the same parameters, it returns the next option and sets the corresponding global variable. If no recognizable option is available -1. getopt()The global variables set include:
  • char *optarg-- Current option parameter string (if any ).
  • int optind-- The current index value of argv. When getopt () is used in a while loop, after the loop ends, the remaining strings are treated as operands, which can be found in argv [optind] To argv [argc-1.
  • Int opterr -- if this variable is not zero, the getopt () function is "invalid option" and "Parameter options are missing, and the error message is output.
  • int optopt-- If an invalid option character is found, the getopt () function or '? 'Character, or return ':' character, and optopt contains the found invalid option characters.
The following procedure is used as an example:
Option:
  • -N -- display "my name ".
  • -G -- display "my girlfriend's name ".
  • -L -- options with parameters.
Listing 2:
# Include <stdio. h>
# Include <unistd. h>

Int main (INT argc, char ** argv)
{
Int OC;/* option character */
Char * B _opt_arg;/* option parameter string */

While (OC = getopt (argc, argv, "NGL :"))! =-1)
{
Switch (OC)
{
Case 'N ':
Printf ("My name is lyong. \ n ");
Break;
Case 'G ':
Printf ("Her name is xxiong. \ n ");
Break;
Case 'l ':
B _opt_arg = optarg;
Printf ("Our love is % s \ n", optarg );
Break;
}
}
Return 0;
}
Running result:
$./Opt_parse_demo-n
My name is lyong.
$./Opt_parse_demo-G
Her name is xxiong.
$./Opt_parse_demo-l forever
Our love is forever
$./Opt_parse_demo-NGL forever
My name is lyong.
Her name is xxiong.
Our love is forever
6. It is inevitable that the calling program that changes getopt ()'s incorrect output behavior of the command line parameter information is incorrect. This error may be caused by Invalid command line options or missing option parameters. Under normal circumstances, getopt () will output its own error information for these two cases and return '? '. To verify this, you can modify the code in Listing 2 above. Listing 3:
# Include <stdio. h>
# Include <unistd. h>

Int main (INT argc, char ** argv)
{
Int OC;/* option character */
Char * B _opt_arg;/* option parameter string */

While (OC = getopt (argc, argv, "NGL :"))! =-1)
{
Switch (OC)
{
Case 'N ':
Printf ("My name is lyong. \ n ");
Break;
Case 'G ':
Printf ("Her name is xxiong. \ n ");
Break;
Case 'l ':
B _opt_arg = optarg;
Printf ("Our love is % s \ n", optarg );
Break;
Case '? ':
Printf ("arguments error! \ N ");
Break;
}
}
Return 0;
}
Enter an incorrect command line and the result is as follows:
$./Opt_parse_demo-l
./Opt_parse_demo: option requires an argument -- l
Arguments error!
If you do not want to output any error messages or want to output custom error messages. You can use the following two methods to change the error message output behavior of the getopt () function:
  1. Before calling getopt (), set opterr to 0. In this way, you can force the getopt () function to output no messages when it finds an error.
  2. If the first character of the optstring parameter is a colon, The getopt () function will remain silent and return different characters according to the error, as shown below:
    • "Invalid option" -- getopt () returns '? And optopt contains invalid option characters (this is a normal behavior ).
    • "Missing option parameter" -- getopt () returns ':'. If the first character of optstring is not a colon, getopt () returns '? ', Which makes this situation different from the case where the invalid option is used.
Listing 4:
# Include <stdio. h>
# Include <unistd. h>

Int main (INT argc, char ** argv)
{
Int OC;/* option character */
Char EC;/* invalid option character */
Char * B _opt_arg;/* option parameter string */

While (OC = getopt (argc, argv, ": NGL :"))! =-1)
{
Switch (OC)
{
Case 'N ':
Printf ("My name is lyong. \ n ");
Break;
Case 'G ':
Printf ("Her name is xxiong. \ n ");
Break;
Case 'l ':
B _opt_arg = optarg;
Printf ("Our love is % s \ n", optarg );
Break;
Case '? ':
EC = (char) optopt;
Printf ("invalid option character \ '% C \'! \ N ", EC );
Break;
Case ':':
Printf ("the option parameter is missing! \ N ");
Break;
}
}
Return 0;
}
Test results:

$./Opt_parse_demo-
Invalid option character 'a '!
$./Opt_parse_demo-l
The option parameter is missing!

Command Line Parameter Parsing function -- getopt ()

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.