Home > Back-end >  Consult getopt_long_only () function of the problem?
Consult getopt_long_only () function of the problem?

Time:01-19

This function can not use capital letters short options?
Look at the code below:
 
# include & lt; Stdlib. H>
# include & lt; String. H>
# include & lt; Math. H>
# include & lt; unistd.h>
# include & lt; The getopt. H>

The static struct option long_options []=
{
{" reqarg ", required_argument, NULL, 'r'},
{" optarg, "optional_argument, NULL, 'o'},
{" noarg1 required_argument, NULL, 'N'},
{" noarg required_argument, NULL, 'n'},
{NULL, 0, NULL, 0},
};

Int main (int arg c, char * argv [])
{
Int opt;
Int digit_optind=0;
Int option_index=0;
Char * string=": A: b: c: N";
While ((opt=getopt_long_only (arg c, argv, string, long_options, & amp; Option_index))!=1)
{
Printf (" opt=% c t \ \ t ", opt);
Printf (" optarg="% s \ \ t t, optarg);
Printf (" optind=% d t \ \ t ", optind);
Printf (" argv [optind]="% s \ \ t t, argv [optind]);
Printf (" option_index=% d \ n ", option_index);
}
}



Results directly on the command line is as follows:

#. - r/t 234-324 - n n asdf
Opt=r optarg=324 optind=3 argv [optind]=-n option_index=0
./t: option '-n' is ambiguous
Opt=? Optarg=(null) optind=4 argv [optind]=234 option_index=0
./t: unrecognized option '-n'
Opt=? Optarg=(null) optind=6 argv [optind]=asdf option_index=0

Why will have this two red font display? Struct option long_options [] doesn't define the capital letters in short option value?

CodePudding user response:

Your problem solved, can share the,
  • Related