En C, getopt_long n'analyse pas les arguments facultatifs en paramètres de paramètres de ligne de commande.getopt n'analyse pas les arguments facultatifs en paramètres
Quand je lance le programme, l'argument optionnel n'est pas reconnu comme l'exemple ci-dessous courir.
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
Voici le code de test.
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv)
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long(argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
printf("You suck ");
if (optarg)
printf (", %s!\n", optarg);
else
printf ("!\n", optarg);
break;
case '?':
printf("Unknown option\n"); break;
}
}
return 0;
}
Je documente ceci ici avec la réponse, ainsi d'autres personnes n'ont pas besoin de se frapper la tête contre le mur. – hayalci