利用ACE_Get_Opt解析命令
ACE_Get_Opt通常用来解析程序的命令行参数,也可结合ACE_ARGV类解析其他的字符串命令.
ACE_TCHAR szCmd[] = ACE_TEXT("ping -l 100 -n 25 -w 10000");
//分解字符串为参数向量,ACE_ARGV的argc()成员函数返回向量数目,argv()返回向量数组
ACE_ARGV argvEnv( szCmd );
ACE_TCHAR szOptions[] = ACE_TEXT (":l:n:w:");
//分析参数对
ACE_Get_Opt cmdOpt( argvEnv.argc(), argvEnv.argv(), szOptions ) ;
int nOption;
while ( ( nOption = cmdOpt () ) != EOF )
{
switch ( nOption )
{
case 'l':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-l值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case 'n':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-n值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case 'w':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-w值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case ':':
break;
}
}
//分解字符串为参数向量,ACE_ARGV的argc()成员函数返回向量数目,argv()返回向量数组
ACE_ARGV argvEnv( szCmd );
ACE_TCHAR szOptions[] = ACE_TEXT (":l:n:w:");
//分析参数对
ACE_Get_Opt cmdOpt( argvEnv.argc(), argvEnv.argv(), szOptions ) ;
int nOption;
while ( ( nOption = cmdOpt () ) != EOF )
{
switch ( nOption )
{
case 'l':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-l值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case 'n':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-n值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case 'w':
{
ACE_DEBUG(( LM_DEBUG, ACE_TEXT("-w值为:%s\n"), cmdOpt.opt_arg () ));
}
break;
case ':':
break;
}
}