Getopt () function description

Source: Internet
Author: User

Getopt (analyze command line parameters)
 
Related functions
 
Header file # include <unistd. h>
 
Define the function 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, set the global variable opterr to 0.
 
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.
 
Example # include <stdio. h>
# Include <unistd. h>
Int main (INT argc, char ** argv)
{
Int ch;
Opterr = 0;
While (CH = getopt (argc, argv, "a: bcde "))! =-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 );
}
 
Run $./getopt-B
Option B: B
$./Getopt-C
Other option: c
$./Getopt-
Other option
$./Getopt-a12345
Option A: '200'/* Self-written test code */# include <string. h>
# Include <stdio. h>
# Include <unistd. h> static int opt_a = 0;
Static int opt_ B = 0;
Static int opt_c = 0;
Static int opt_d = 0;
Static int opt_e = 0;
Static char * opt_a_arg = NULL; static void usage ()
{
Fprintf (stderr, "Usage: getopt [A Arg] [B] [C] [d] [e]/n ");
Exit (1);} int main (INT argc, char ** argv)
{
Int OPT;
Char opts [] = "A: bcde ";
Opterr = 0;
While (OPT = getopt (argc, argv, opts ))! =-1 ){
Switch (OPT)
{
Case 'A ':
Opt_a = 1;
Opt_a_arg = strdup (optarg );
Break;
Case 'B ':
Opt_ B = 1;
Break;
Case 'C ':
Opt_c = 1;
Break;
Case 'D ':
Opt_d = 1;
Break;
Case 'E ':
Opt_e = 1;
Break;
Case '? ':
Usage ();
Break;
}
} If (opt_a | opt_ B | opt_c | opt_d | opt_e)
{
If (opt_a)
Printf ("opt a is set and Arg is % s/n", opt_a_arg );
If (opt_ B)
Printf ("opt B is set/N ");
If (opt_c)
Printf ("opt C is set/N ");
If (opt_d)
Printf ("opt D is set/N ");
If (opt_e)
Printf ("opt e is set/N ");
} Else
Usage ();}/* test code in the getopt source code file of glib C */# ifdef test/* compile with-Dtest to make an executable for use in testing
The above definition of 'getopt'. */int
Main (INT argc, char ** argv)
{
Int C;
Int digit_optind = 0; while (1)
{
Int this_option_optind = optind? Optind: 1; C = getopt (argc, argv, "ABC: D: 0123456789 ");
If (C =-1)
Break; Switch (c)
{
Case '0 ':
Case '1 ':
Case '2 ':
Case '3 ':
Case '4 ':
Case '5 ':
Case '6 ':
Case '7 ':
Case '8 ':
Case '9 ':
If (digit_optind! = 0 & digit_optind! = This_option_optind)
Printf ("Digits occur in two different argv-elements./N ");
Digit_optind = this_option_optind;
Printf ("option % C/N", C );
Break; Case 'A ':
Printf ("option A/N ");
Break; Case 'B ':
Printf ("option B/N ");
Break; Case 'C ':
Printf ("option C with value '% s'/N", optarg );
Break; case '? ':
Break; default:
Printf ("?? Getopt returned character code 0% o ?? /N ", C );
}
} If (optind <argc)
{
Printf ("non-option argv-elements :");
While (optind <argc)
Printf ("% s", argv [optind ++]);
Printf ("/N ");
} Exit (0 );
} # Endif/* test */

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.