2017-07-14 27 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这两种解决方案都没有问题。 –