zoukankan      html  css  js  c++  java
  • [zz]使用 Google gflags 简化命令行参数处理

    (本文章仅适用于C++程序)
    写服务程序时,如果需要提供命令行参数。传统的方法是手工解析argv参数,或者使用getopt函数。两种方法都比较费劲。使用Google gflags可以大大简化命令行参数处理。
    安装gflag
    从官方地址http://code.google.com/p/google-gflags/下载gflags并安装。比如我下载的是1.5版本。
    [yichi@yichi tmp]$ tar zxvf gflags-1.5.tar.gz
    [yichi@yichi tmp]$ cd gflags
    [yichi@yichi gflags-1.5]$ ./configure –prefix=$HOME/google-library
    [yichi@yichi gflags-1.5]$ make j=4 & make install
    使用gflags
    main.cc
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    // Copyright (c) 2011, D-Media Communication Technology Inc.
    // All rights reserved.
    //
    //
    // ---
    // Author: Yichi Zhang <zyichi@gmail.com>
    //
    // File content description here
     
    #include "server.h"
     
    #include <google/gflags.h>
     
    #include <string>
     
    #include <stdio.h>
     
    DEFINE_bool(memory_pool, false, "If use memory pool");
    DEFINE_bool(daemon, true, "If started as daemon");
    DEFINE_string(module_id, "", "Server module id");
     
    DEFINE_int32(http_port, 80, "HTTP listen port");
    DEFINE_int32(https_port, 443, "HTTPS listen port");
     
    int
    main(int argc, char** argv)
    {
      ::google::ParseCommandLineFlags(&argc, &argv, true);
     
      // XXX NOTE: Should add 'FLAGS_' prefix.
      printf("Server module id: %s\n", FLAGS_module_id.c_str());
     
      if (FLAGS_daemon) {
        printf("Run as daemon: %d\n", FLAGS_daemon);
      }
      if (FLAGS_memory_pool) {
        printf("Use memory pool: %d\n", FLAGS_daemon);
      }
     
      Server server;
     
      return 0;
    }
    server.h
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    // Copyright (c) 2011, D-Media Communication Technology Inc.
    // All rights reserved.
    //
    //
    // ---
    // Author: Yichi Zhang <zyichi@gmail.com>
    //
    // File content description here
     
    #include <google/gflags.h>
     
    #include <stdio.h>
     
    // Declare, defined in 'main.cc'.
    DECLARE_int32(http_port);
    DECLARE_int32(https_port);
     
    class Server {
     public:
      Server() {
        // XXX NOTE: Should add 'FLAGS_' prefix.
        printf("HTTP listen port: %d\n", FLAGS_http_port);
        printf("HTTPS listen port: %d\n", FLAGS_https_port);
      }
      virtual ~Server() {}
     
     private:
      ;
    };
    编译示例程序
    [yichi@yichi gflags-sample]$ g++ -o gflags-sample main.cc -I $HOME/google-library/include -L $HOME/google-library/lib -lgflags
    运行示例程序
    带参数运行
    [yichi@yichi gflags-sample]$ LD_LIBRARY_PATH=$HOME/google-library/lib:$LD_LIBRARY_PATH ./gflags-sample
    Server module id:
    Run as daemon: 1
    HTTP listen port: 80
    HTTPS listen port: 443
    带参数运行,使用等号赋值
    [yichi@yichi gflags-sample]$ LD_LIBRARY_PATH=$HOME/google-library/lib:$LD_LIBRARY_PATH ./gflags-sample -module_id="server_007" -daemon=true -http_port=8080 -https_port=4443
    Server module id: server_007
    Run as daemon: 1
    HTTP listen port: 8080
    HTTPS listen port: 4443
    带参数运行,使用空格分割参数和值
    [yichi@yichi gflags-sample]$ LD_LIBRARY_PATH=$HOME/google-library/lib:$LD_LIBRARY_PATH ./gflags-sample -module_id "server_007" -daemon true -http_port 8080 -https_port 4443
    Server module id: server_007
    Run as daemon: 1
    HTTP listen port: 8080
    HTTPS listen port: 4443
    显示帮助
    [yichi@yichi gflags-sample]$ LD_LIBRARY_PATH=$HOME/google-library/lib:$LD_LIBRARY_PATH ./gflags-sample –help
    gflags-sample: Warning: SetUsageMessage() never called
    Flags from main.cc:
    -daemon (If started as daemon) type: bool default: true
    -http_port (HTTP listen port) type: int32 default: 80
    -https_port (HTTPS listen port) type: int32 default: 443
    -memory_pool (If use memory pool) type: bool default: false
    -module_id (Server module id) type: string default: ""
    参考资料
  • 相关阅读:
    进程、线程和协程的图解
    Python多线程的原理与实现
    Python多进程原理与实现
    python面试题-1
    数据库事务隔离级别--读未提交,读已提交,重复读,序列化
    java--浅谈线程(二、线程的方法和状态)
    java--浅谈线程(一简单介绍)
    类加载机制--浅谈
    JSP/Servlet Web 学习笔记 DayThree
    JSP/Servlet Web 学习笔记 DayThree —— 实现一个登陆小界面
  • 原文地址:https://www.cnblogs.com/zhangzhang/p/2400669.html
Copyright © 2011-2022 走看看