Use getopt to process parameters of the main function

Source: Internet
Author: User
Environment: Fedora12Gcc4.4.2 in C language, the main function declaration is as follows: intmain (intargc, char * argv []); argc refers to the number of program parameters, including the program name itself, if the program does not contain parameters, argc is 1. Each array element of argv stores one program parameter, and the program name is stored in argv [0]. For example: $

Environment:

Fedora 12

Gcc4.4.2 

 

In C, the main function is declared as follows:

Int main(Int argc, char * argv []);

Argc refers to the number of program parameters, including the program name itself. If the program does not contain parameters, argc is 1;

Each array element of argv stores a program parameter, and the program name is stored in argv [0];

For example:

$ Ls-l

In this case, argc is 2, argv [0] is ls, and argv [1] is-l. 

 

Program parameters can be divided into three types: Options, associated values of options, and non-option parameters. For example:

$ Gcc hello. c-o hello
Hello. c is a non-option parameter,-o is an option, and hello is the associated value of the-o option.

According to Linux's practice, program options should start with a dash, followed by a single letter or number. There are two options: with and without associated values, for example:

 

$ Gcc hello. c-o hello

Option-o must contain an associated value.

$ Ls-l

Option-l does not require parameters.

Options without associated values can be combined after a dash, for example:

$ Ls-la 

 

Linux provides the getopt function, which is used to process program parameters according to the preceding rules. The related definitions are as follows:

# Include

 

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

Extern char * optarg;

Extern int optind, opterr, optopt; 

 

Getopt uses the argc and argv of the main function as the first two parameters. optsting is a character list. Each character represents a single character option. If a character is followed by a colon (:), indicates that the character has an associated value as the next parameter;

 

The returned value of getopt is the next option parameter in the argv array. The optind records the subscript of the argv array. If the option parameter is processed, the function returns-1;

If an unrecognized option is displayed, the question mark (?) is returned (?), And save it in optopt;

If an option requires an associated value and the program is not provided during execution, a question mark (?) is returned (?), If you set the first character of optstring to colon (:), the function returns a colon instead of a question mark.

 

After option parameters are processed, optind points to other non-option parameters at the end of the argv array. In fact, getopt will rearrange the argv array during execution and move the non-option parameter to the end of the array. 

 

Example:

The following program provides two options for non-correlated values:-v,-g; an option for associating values:-t; and a non-option parameter.

  1. // Getopttest. c
  2. # Include
  3. # Include
  4. IntMain (IntArgc,Char* Argv [])
  5. {
  6. IntOpt = 0;
  7. IntI = 0;
  8. Const Char* Optstring =": Vgt :";
  9. // Number of non-option Parameters
  10. Const IntNum = 1;
  11. // Print the argv array before calling getopt
  12. For(I = 0; I
  13. Printf ("% D: % s/n", I, argv [I]);
  14. // Processing option Parameters
  15. While(Opt = getopt (argc, argv, optstring ))! =-1)
  16. {
  17. Switch(Opt)
  18. {
  19. Case 'V':
  20. Case 'G':
  21. Printf ("Option: % c/n", Opt );
  22. Break;
  23. Case 'T':
  24. Printf ("Option: % c = % s/n", Opt, optarg );
  25. Break;
  26. Case ':':
  27. Printf ("The option need a value/n");
  28. Break;
  29. Case '? ':
  30. Printf ("Unknow option: % c/n", Optopt );
  31. Break;
  32. }
  33. }
  34. // Process non-option Parameters
  35. For(I = 0; optind
  36. {
  37. If(I < span><>
  38. Printf ("Argument: % s/n", Argv [optind]);
  39. Else
  40. Printf ("Excess argument: % s/n", Argv [optind]);
  41. }
  42. // Print the argv array after getopt is called
  43. For(I = 0; I
  44. Printf ("% D: % s/n", I, argv [I]);
  45. Return0;
  46. } 

Compile:Gcc-Wall getopttest. c-o getopttest

Run:

$./Getopttest arg1-vg-t value-x arg2

0:./getopttest

1: arg1

2:-vg

3:-t

4: value

5:-x

6: arg2

Option:V

Option:G

Option: t = value

Unknow option:X

Argument: arg1

Excess argument: arg2

0:./getopttest

1:-vg

2:-t

3: value

4:-x

5: arg1

6: arg2

 

The execution result shows that the getopt function rearranges the argv array and ranks the non-option parameter arg1 at the end of the array.

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.