歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++實現的命令行參數管理

C++實現的命令行參數管理

日期:2017/3/1 10:06:24   编辑:Linux編程

在用C++編寫可運行程序時,經常需要輸入除了可運行文件之外的其它的命令行參數,可以用傳統的getopt函數來分析,本文基於面向對象,分析一種管理命令行參數方法 -- 來源於webrtc項目,在閱讀過程中,大家分享一下。

一,傳統命令行分析

包含頭文件:

  1. #include<unistd.h>
  2.   int getopt(int argc,char * const argv[ ],const char * optstring);
  3.   extern char *optarg;
  4.   extern int optind, opterr, optopt;

二,命令行參數管理

假設命令行的輸入格式的規則如下:

  • --flag 布爾類型。
  • --noflag 布爾類型。
  • --flag=value 等號周邊沒有空格。

2.1 參數的值封裝---FlagValue

這個類對參數的值進行封裝,如--prefix=/usr,作為一個命令行參數時,prefix為鍵,/usr為值。在參數中,在此定義值的類型為布爾、整型、浮點、字符串中的一種。

由於一個值在只能取四種的一種,所以此處用聯合類型表示FlagValue。

  1. union FlagValue {
  2. static FlagValue New_BOOL(int b) {
  3. FlagValue v;
  4. v.b = (b != 0);
  5. return v;
  6. }
  7. static FlagValue New_INT(int i) {
  8. FlagValue v;
  9. v.i = i;
  10. return v;
  11. }
  12. static FlagValue New_FLOAT(float f) {
  13. FlagValue v;
  14. v.f = f;
  15. return v;
  16. }
  17. static FlagValue New_STRING(const char* s) {
  18. FlagValue v;
  19. v.s = s;
  20. return v;
  21. }
  22. bool b;
  23. int i;
  24. double f;
  25. const char* s;
  26. };

這個聯合類型對命令行中鍵值對中的值進行封裝,可以表示四種類型。

2.2 命令行中鍵值的表示 -- Flag

這個類是表示一對鍵值的抽象,包含下列元素:

  • 鍵值對。包括name和variable_表示鍵和值。如--prefix=/usr中,name的值為prefix,variable_為/usr的一個表示。
  • 鏈表維護域。Flag *next_用於指向下一個命令行參數。
  • comment_表示該參數的解釋。
  • file表示和鍵值相關的外部文件。
  • default_表示默認情況下,就是用戶沒有輸入該參數的情況下默認的值。
  • 定義友元類FlagList,因為FlagList需要訪問Flag的next_域。
  1. class Flag {
  2. public:
  3. enum Type { BOOL, INT, FLOAT, STRING };
  4. // Internal use only.
  5. Flag(const char* file, const char* name, const char* comment,
  6. Type type, void* variable, FlagValue default_);
  7. // General flag information
  8. const char* file() const { return file_; }
  9. const char* name() const { return name_; }
  10. const char* comment() const { return comment_; }
  11. // Flag type
  12. Type type() const { return type_; }
  13. // Flag variables
  14. bool* bool_variable() const {
  15. assert(type_ == BOOL);
  16. return &variable_->b;
  17. }
  18. int* int_variable() const {
  19. assert(type_ == INT);
  20. return &variable_->i;
  21. }
  22. double* float_variable() const {
  23. assert(type_ == FLOAT);
  24. return &variable_->f;
  25. }
  26. const char** string_variable() const {
  27. assert(type_ == STRING);
  28. return &variable_->s;
  29. }
  30. // Default values
  31. bool bool_default() const {
  32. assert(type_ == BOOL);
  33. return default_.b;
  34. }
  35. int int_default() const {
  36. assert(type_ == INT);
  37. return default_.i;
  38. }
  39. double float_default() const {
  40. assert(type_ == FLOAT);
  41. return default_.f;
  42. }
  43. const char* string_default() const {
  44. assert(type_ == STRING);
  45. return default_.s;
  46. }
  47. // Resets a flag to its default value
  48. void SetToDefault();
  49. // Iteration support
  50. Flag* next() const { return next_; }
  51. // Prints flag information. The current flag value is only printed
  52. // if print_current_value is set.
  53. void Print(bool print_current_value);
  54. private:
  55. const char* file_;
  56. const char* name_;
  57. const char* comment_;
  58. Type type_;
  59. FlagValue* variable_;
  60. FlagValue default_;
  61. Flag* next_;
  62. friend class FlagList; // accesses next_
  63. };
Copyright © Linux教程網 All Rights Reserved