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.
- // Getopttest. c
- # Include
- # Include
- IntMain (IntArgc,Char* Argv [])
- {
- IntOpt = 0;
- IntI = 0;
- Const Char* Optstring =": Vgt :";
- // Number of non-option Parameters
- Const IntNum = 1;
- // Print the argv array before calling getopt
- For(I = 0; I
- Printf ("% D: % s/n", I, argv [I]);
- // Processing option Parameters
- While(Opt = getopt (argc, argv, optstring ))! =-1)
- {
- Switch(Opt)
- {
- Case 'V':
- Case 'G':
- Printf ("Option: % c/n", Opt );
- Break;
- Case 'T':
- Printf ("Option: % c = % s/n", Opt, optarg );
- Break;
- Case ':':
- Printf ("The option need a value/n");
- Break;
- Case '? ':
- Printf ("Unknow option: % c/n", Optopt );
- Break;
- }
- }
- // Process non-option Parameters
- For(I = 0; optind
- {
- If(I < span><>
- Printf ("Argument: % s/n", Argv [optind]);
- Else
- Printf ("Excess argument: % s/n", Argv [optind]);
- }
- // Print the argv array after getopt is called
- For(I = 0; I
- Printf ("% D: % s/n", I, argv [I]);
- Return0;
- }
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.