网络服务器软件开发/中间件开发,关注ACE/ICE/boost

C++博客 首页 新随笔 联系 聚合 管理
  152 Posts :: 3 Stories :: 172 Comments :: 0 Trackbacks

   通过命令行传递参数在服务器程序中是很常见的,ACE提供了ACE_Get_Opt类以简化该操作,比较简单:

#include 
<ace/Get_Opt.h>
#include 
<string>
#include 
<iostream>
using namespace std;

void usage()
{    
    cout 
<< " 参数错误 " << endl;
    cout 
<< " -h : 服务器IP" << endl;
    cout 
<< " -p : 服务器端口" << endl;
    cout 
<< " -d : 以调试模式运行" << endl;    
}


int main(int argc, char *argv[])
{
    
if (argc < 2)
    
{
        usage();
        
return 1;
    }

    ACE_Get_Opt cmdline (argc, argv, 
"h:p:d");

    
int cnt = 0;//必备参数计数器
    string host = "";
    
int port = 0;
    
bool debug = false;
    
int cmd = 0;
    
while ((cmd = cmdline()) != -1)
    
{
        
switch (cmd)
        
{
        
case 'h':
            
{
                host 
= cmdline.opt_arg();
                cout 
<< "host : " << host << endl;
                
++cnt;
                
break;
            }

        
case 'p':
            
{
                port 
= ACE_OS::atoi(cmdline.opt_arg());
                cout 
<< "port : " << port << endl;
                
++cnt;
                
break;
            }

        
case 'd':
            
{
                debug 
= true;
                cout 
<< "debug mode" << endl;
                
break;
            }

        
default :
            
{
                usage();
                
return 1;
            }

            
        }

    }


    
if (cnt < 2)
    
{
        usage();
        
return 1;
    }


    
return 0;
}
;
posted on 2010-11-09 23:53 true 阅读(1008) 评论(0)  编辑 收藏 引用 所属分类: ACE

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理