2013-02-08 25 views
0

我需要檢查以下參數:getopt_long_only()不工作

./centro -n CD1 –cp 100000 –i 100000 –t 30 –s 1000 –p 11111 

而且他們可以來以任何順序。我有以下代碼:

void checkParameters (int argc, char** argv, center_data* info) { 

    int opt = 0; 

    const char* const short_options = "n:i:t:s:p:a:"; 

    static struct option long_options[] = { 
        {"cp", 1, NULL, 'a'}, 
        {0, 0 , 0, 0} 
    }; 

    if(argc != 13) 
      error("Error en numero de argumentos \n"); 

    while(opt != -1){ 

     opt = getopt_long_only(argc, argv, short_options, long_options, NULL); 

     switch (opt){ 

      case 'n': 
       strcpy(info->center_name, optarg); 
       break; 

      case 'a': 
       printf("el optgar %s \n", optarg); 
       info->cap_max = atoi(optarg); 
       if (!(38000 <= info->cap_max && info->cap_max <= 3800000)) 
        error("Error en la capacidad maxima del centro. \n"); 

       break; 

      case 'i': 
       printf("el optgar %s \n", optarg); 
       info->stock = atoi(optarg); 
       break; 

      case 't': 
       printf("el optgar %s \n", optarg); 
       info->response_time = atoi(optarg); 
       if (!(0 <= info->response_time && info->response_time <= 180)) 
        error("Error en el tiempo de respuesta \n"); 

       break; 

      case 's': 
       printf("el optgar %s \n", optarg); 
       info->supply = atoi(optarg); 
       if(!(0 <= info->supply && info->supply <= 10000)) 
        error("Error en la cantidad de suministro \n"); 

       break; 

      case 'p': 
       printf("el optgar %s \n", optarg); 
       info->port = atoi(optarg); 
       if (0 <= info->port && info->port <=1023) 
        error("Error: se esta utilizando un puerto bien conocido"); 

       break; 

      case -1: 
       printf("caso -1\n"); 
       break; 

      default: 
       printf("caso default\n"); 
       abort(); 
     } 
    } 

    if (!(0 <= info->stock && info->stock <= info->cap_max)) 
     error("Error en el inventario actual de la bomba \n"); 
} 

事情是,它進入第一個案例,然後-1,它退出。這是沒有意義的。我看不出有什麼錯誤,在使用getopt_long_only

回答

3

注意在不同長度的破折號:

./centro -n CD1 –cp 100000 –i 100000 –t 30 –s 1000 –p 11111 

第一個是減去字符。其他人更長。那就是問題所在。您沒有使用減去所有參數

+0

我明白了,那真是個愚蠢的錯誤。感謝讓我看到錯誤。 – Alessandroempire

0

先生

試試這個命令:

./centro -a 100 -n 100000 -i 100000 -t 30 -s 1000 -p 11111 

順便說一句,你可以請讓我們知道它是哪個角色,我找不到任何這樣在我的鍵盤。