需要有关如何设置getopt_long以正确传递命令行参数的建议

need suggestions on how to setup getopt_long to properly pass command line arguments

本文关键字:命令行 参数 long 何设置 getopt 设置      更新时间:2023-10-16

伙计们, 我在这里回顾了使用get_opt_long的示例:https://linux.die.net/man/3/getopt_long_only 而且我仍然对如何在我的情况下使用感到困惑。我有多种选择。

-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,-As作为命令行参数。我被迫在我的所有选择中使用统一字符。

我的同时块有

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

谢谢

两种可能的解决方案:

  1. "Aa""As""Af"使用长参数
  2. 使用一个短参数'A'该参数采用必需的参数,即'a''s''f'字符