使用 Google gflags 簡化命令列參數處理

來源:互聯網
上載者:User
(本文章僅適用於C++程式)
寫服務程式時,如果需要提供命令列參數。傳統的方法是手工解析argv參數,或者使用getopt函數。兩種方法都比較費勁。使用Google gflags可以大大簡化命令列參數處理。安裝gflag從官方地址http://code.google.com/p/google-gflags/下載gflags並安裝。比如我下載的是1.5版本。
[yichi@yichi tmp]$ wget http://google-gflags.googlecode.com/files/gflags-1.5.tar.gz[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範例程式碼:http://www.faithbee.com/wp-content/uploads/2011/05/gflags-sample.tar.bz2main.cc
// 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"); intmain(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

// 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-sampleServer module id:Run as daemon: 1HTTP listen port: 80HTTPS 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=4443Server module id: server_007Run as daemon: 1HTTP listen port: 8080HTTPS 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 4443Server module id: server_007Run as daemon: 1HTTP listen port: 8080HTTPS listen port: 4443
顯示協助
[yichi@yichi gflags-sample]$ LD_LIBRARY_PATH=$HOME/google-library/lib:$LD_LIBRARY_PATH ./gflags-sample –helpgflags-sample: Warning: SetUsageMessage() never calledFlags 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: ""
參考資料Google gflags官方文檔:http://google-gflags.googlecode.com/svn/trunk/doc/gflags.html from:http://www.faithbee.com/?p=415

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.