一个带命令行解析功能的console程序框架

这个程序的代码对于很多linux程序员应该是很熟悉的,仿照linux实现了 命令行参数解析器,所以提供一个框架程序,以后再写类似代码的时候直接在本框架的基础上修改就是了。
[csharp] view plain copy
  1. #include "GetOpt.h"  
  2.  
  3. #include<iostream>  
  4.   
  5.   
  6.   
  7. using namespace std;  
  8.   
  9.   
  10.   
  11. const char* program_name;  
  12.   
  13.   
  14.   
  15. void print_usage ()  
  16.   
  17. {  
  18.   
  19.     cout << "Usage: %s options [outputfile ...]" << endl;  
  20.   
  21.     cout << " -h --help Display this usage information." << endl;  
  22.   
  23.     cout << " -o --output filename Write output to file." << endl;  
  24.   
  25.     cout << " -v --verbose Print verbose messages." << endl;  
  26.   
  27. }  
  28.   
  29.  
  30.   
  31. int main (int argc, char* argv[])  
  32.   
  33. {  
  34.   
  35.     int opt;  
  36.   
  37.     const charconst short_options = "ho:v";   
  38.   
  39.     const struct option long_options[] = {  
  40.   
  41.         { "help",         no_argument,          NULL,   'h' },  
  42.   
  43.         { "output",     required_argument,  NULL,   'o' },  
  44.   
  45.         { "verbose",   no_argument,          NULL,   'v' },  
  46.   
  47.         { NULL,         no_argument,           NULL,   0 }  
  48.   
  49.     };  
  50.   
  51.   
  52.   
  53.     int verbose = 0;  
  54.   
  55.     program_name = argv[0];  

  56.     opt = getopt_long (argc, argv, short_options,long_options, NULL);  
  57.   
  58.     if(opt == -1)  
  59.   
  60.     {  
  61.   
  62.         print_usage();  
  63.   
  64.         exit(0);  
  65.   
  66.     }  
  67.   
  68.     while(opt != -1)  
  69.   
  70.     {  
  71.   
  72.         switch (opt)  
  73.   
  74.         {  
  75.   
  76.             case 'o':  
  77.   
  78.                 cout << "outputfile is: " << optarg << endl;   
  79.   
  80.                 break;  
  81.   
  82.   
  83.   
  84.             case 'v':   
  85.   
  86.                 cout << "print verbose infomation" << endl;  
  87.   
  88.                 break;  
  89.   
  90.   
  91.   
  92.             case '?':  
  93.   
  94.                 cout << "sytax error: no this argument: " << static_cast<char>(optopt) << endl;  
  95.   
  96.                 print_usage();  
  97.   
  98.                 exit(0);  
  99.   
  100.   
  101.   
  102.             case 'h':   
  103.   
  104.                 print_usage ();  
  105.   
  106.                 break;  
  107.   
  108.   
  109.   
  110.             default:   
  111.   
  112.                 break;  
  113.   
  114.         }  
  115.   
  116.         opt = getopt_long (argc, argv, short_options,long_options, NULL);  
  117.   
  118.     }  
  119.   
  120.     return 0;  
  121.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值