Linux console

使用的是关键的函数getopt_long

  1. #include <unistd.h>   
  2.   
  3.        int getopt(int argc, char * const argv[],   
  4.                   const char *optstring);   
  5.   
  6.        extern char *optarg;   
  7.        extern int optind, opterr, optopt;   
  8.   
  9.        #include <getopt.h>   
  10.   
  11.        int getopt_long(int argc, char * const argv[],   
  12.                   const char *optstring,   
  13.                   const struct option *longopts, int *longindex);   
  14.   
  15.        int getopt_long_only(int argc, char * const argv[],   
  16.                   const char *optstring,   
  17.                   const struct option *longopts, int *longindex);
    • #include "GetOpt.h"   
    •   
    • #include<iostream>   
    •   
    •   
    •   
    • using namespace std;   
    •   
    •   
    •   
    • const char* program_name;   
    •   
    •   
    •   
    • void print_usage ()   
    •   
    • {   
    •   
    •   
    •   
    •     cout << "Usage: %s options [outputfile ...]" << endl;   
    •   
    •   
    •   
    •     cout << " -h --help Display this usage information." << endl;   
    •   
    •     cout << " -o --output filename Write output to file." << endl;   
    •   
    •     cout << " -v --verbose Print verbose messages." << endl;   
    •   
    •   
    •   
    • }   
    •   
    •   
    •   
    • int main (int argc, char* argv[])   
    •   
    • {   
    •   
    •     int opt;   
    •   
    •      
    •   
    •     const charconst short_options = "ho:v";   
    •   
    •   
    •   
    •     const struct option long_options[] = {   
    •   
    •         { "help",         no_argument,          NULL,   'h' },   
    •   
    •         { "output",     required_argument,  NULL,   'o' },   
    •   
    •         { "verbose",   no_argument,          NULL,   'v' },   
    •   
    •         { NULL,         no_argument,           NULL,   0 }   
    •   
    •     };   
    •   
    •   
    •   
    •     int verbose = 0;   
    •   
    •   
    •   
    •     program_name = argv[0];   
    •   
    •   
    •   
    •     opt = getopt_long (argc, argv, short_options,long_options, NULL);   
    •   
    •   
    •   
    •     if(opt == -1)   
    •   
    •     {   
    •   
    •         print_usage();   
    •   
    •         exit(0);   
    •   
    •     }   
    •   
    •     while(opt != -1)   
    •   
    •     {   
    •   
    •         switch (opt)   
    •   
    •         {   
    •   
    •             case 'o':   
    •   
    •                 cout << "outputfile is: " << optarg << endl;    
    •   
    •                 break;   
    •   
    •   
    •   
    •             case 'v':    
    •   
    •                 cout << "print verbose infomation" << endl;   
    •   
    •                 break;   
    •   
    •   
    •   
    •             case '?':   
    •   
    •                 cout << "sytax error: no this argument: " << static_cast<char>(optopt) << endl;   
    •   
    •                 print_usage();   
    •   
    •                 exit(0);   
    •   
    •   
    •   
    •             case 'h':    
    •   
    •                 print_usage ();   
    •   
    •                 break;   
    •   
    •   
    •   
    •             default:    
    •   
    •                 break;   
    •   
    •         }   
    •   
    •         opt = getopt_long (argc, argv, short_options,long_options, NULL);   
    •   
    •     }   
    •   
    •   
    •   
    •     return 0;   
    •   
    • }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值