In the process of inevitable need to use command-line options, you can choose to parse the command-line options, but there are ready-made, why rebuild the wheel. The following describes the use of getopt_long_only and Getopt_long (roughly the same) to resolve command-line options.
The main use of the program:
Short options |
Long option |
Whether you want parameters |
-N |
--username |
Yes (user name) |
Specify User name |
-D |
--debug |
Whether |
Have you tested |
1. Function Source
#include <getopt.h>//getopt_long () header file position
int getopt_long (int ___argc, char *const *___argv,
const char *_ _shortopts,
const struct option *__longopts, int *__longind);
int getopt_long_only (int ___argc, char *const *___argv,
const char *__shortopts,
2, Parameter introduction ARGC argv: Pass directly from main function to shortopts: Short option string. As "n:v", it should be pointed out that the short option string does not need '-', but the option needs to pass the parameter, after the short option followed by ":". Longopts:struct option Array, for holding long option arguments. Longind: Used to return the index value of a long option in an array of longopts structures for debugging. General null below describes struct option
struct option
{
const char *name;//long option name
int has_arg;//requires parameter
int *flag;
int val;
};
Name: Long option names
Has_arg: Parameters are required. The value has three different cases
# define No_argument 0 //No parameters required
# define Required_argument 1 ///parameter must be specified
# define Optional_ Argument 2 //parameter optional
Flag and Val depend on each other mainly in two situations: (1), flag for the Null,val value to determine the long option, so you need to specify a unique Val value for the long option. A bridge is also built for long options and short options. (2), flag is not NULL, the Val value is stored in the storage space that the flag points to, identifying the long option that appears.
3, return valueUse short options in your program to return a short option character (such as ' n '), and then save the argument to Optarg before returning it when the parameter is required. The long option is used in the program, and the return value is determined by flag and Val. Returns the Val value when flag is null. So different processing is done according to the Val value, which also shows that Val must be unique. When the Val value is equal to the short option value, the short option resolves the long option for the function, and when flag is not NULL, the Val value is stored in the storage point to flag, Getopt_long returns 0 with undefined long options or short options, Getopt_long returns. Parsing completed, Getopt_long return-1
4. ExampleTheory should be combined with reality
Example One:
#include <stdio.h> #include <stdlib.h> #include <getopt.h>//getopt_long () header file location
int main (int argc, char** argv) {const char *optstring= "N:V";
int c,deb,index; struct option opts[]={{"username", Required_argument,null, ' n '}, {"Version", No_argument,null
, ' V '}, {"Debug", no_argument,&deb,1}, {0,0,0,0}}; while ((C=getopt_long_only (Argc,argv,optstring,opts,&index))!=-1) {switch
(c) {case ' n '://-n or--username specified user name printf ("username is%s\n", optarg);
Break
Case ' V '://-v or--version, output version number printf ("version is 0.0.1 \ n");
Break
Case 0://flag NOT NULL printf ("Debug is%d\n", Deb);
Break Case '?':/
/option does not define printf ("? \ n");
Break
default:printf ("C is%d\n", c);
Break
} return 0; }
Example Two:
#include <stdio.h> #include <stdlib.h> #include <getopt.h>//getopt_long () header file location int main (int argc, Cha
r** argv) {const char *optstring= "N:V";
int c,deb,index;
struct option opts[]={{"username", required_argument,0,0}, {"N", required_argument,0,0}, {"Version", no_argument,0,0}, {"V", no_argument,0,0}, { "Debug", no_argument,0,0}, {"D", no_argument,0,0}, {"Help", no_argument,0
, 0}, {"H", no_argument,0,0}}; while ((C=getopt_long_only (Argc,argv,optstring,opts,&index))!=-1) {switch (index) {//-n or--user
Name Case 0:case 1:printf ("username:%s\n", Optarg);
Break
-V or--version case 2:case 3:printf ("version:1.0.0\n");
BreakD or--debug case 4:case 5:printf ("debug:yes\n");
Break
-H or--help case 6:case 7:printf ("help:?\n");
Break
default:printf ("other:%d\n", index);
Break
} return 0;
}
As for the running result, I only give the use case, everybody is interested may try.
Instance 1:./test-n Boy
Instance 2:./test-n Boy