2017-07-14 34 views
0

Folks, 我在這裏使用了get_opt_long的示例:https://linux.die.net/man/3/getopt_long_only 我仍然對如何在我的情況下使用感到困惑。在我的情況下,我有多個選項。需要關於如何設置getopt_long以正確傳遞命令行參數的建議

-Aa => ask for all 
-As => ask for stats 
-Af => ask for file 
-seed => pass seed 
-num => repeat times 

我能得到-seed -NUM和工作,但不知道如何把-Ap,-Ax -Af

這裏是我的選擇結構:

enter code here 
{"seed"    , required_argument , NULL , 's'} , 
{"num"    , required_argument , NULL , 'n'} , 
{"ask_all"   , no_argument  , NULL , 'a'} , 
{"ask_stat"   , no_argument  , NULL , 't'} , 
{NULL    , 0     , NULL , 0} 

也,我如何使用-Ap, - 作爲命令行參數。我強迫所有選項都使用unic字符。

我而塊具有

case 's': 
    seed = atoi(optarg); 
    break; 
case 'n': 
    num = atoi(num); 
case 'a': 
    ask->all = true; 
    break; 

感謝

+0

我建議您爲您的問題添加其他相關標籤。對於C++用戶來說,使用'getopt'進行命令行解析是很少見的,因爲C++領域還有其他更好的庫 – WhiZTiM

+0

@WhiZTiM argp?您對多個選項的評論提示。 –

+0

你可以提供一些選擇嗎? – Romeo

回答

1

兩個可能的解決方案:

  1. 使用長論據"Aa""As""Af"
  2. 使用一個短參數'A',這需要需要的參數是'a''s''f'字符
+0

問題是我想同時使用「ask_all」和「Aa」作爲參數。可能是我可以爲他們複製線。 – Romeo

+0

@Romeo這兩種解決方案都沒有問題。 –