| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
key_zhou
9年前发布

C++ 简单的命令行参数阅读器

来自: https://helloacm.com/c-simple-command-line-parameters-reader/

Inthis post, the C# version of Command Line Parameter Reader is given and you should see how easy to manipulate the strings in C#. The C++ version is also very powerful in handling strings using the std library.

C++ Coding Exercise: This is a useful class to handle the command line parameters (as key-value pairs).

/*      Author: https://helloacm.com      Free to Use, donation is appreciated:  https://helloacm.com/out/paypal  */     #include <iostream>  #include <map>  #include <vector>  #include <algorithm>     using namespace std;     class SimpleCommandLineReader {  public:      SimpleCommandLineReader(int, char**);      SimpleCommandLineReader(int, char**, bool);      string Get(string);      string Get(string, string);     private:      const char SepChar = '=';      vector<string> _args;      map<string, string> _dict;      bool _caseSensitive = false;      void process(void);  };     SimpleCommandLineReader::SimpleCommandLineReader(int argc, char** argv): SimpleCommandLineReader(argc, argv, false)  {     };     SimpleCommandLineReader::SimpleCommandLineReader(int argc, char** argv, bool sensitive){      _caseSensitive = sensitive;      for (int i = 0; i < argc; i ++) {          string cur(argv[i]);          _args.push_back(cur);      }      process();  };     void SimpleCommandLineReader::process(void) {      for (int i = 0; i < _args.size(); i ++) {          string cur = _args[i];          string key = "";          string val = "";          for (int j = 0; j < cur.length(); j ++) {              if (cur[j] == SepChar) {                  key = cur.substr(0, j);                  val = cur.substr(j + 1);                  break;              }          }          if (!_caseSensitive) {              std::transform(key.begin(), key.end(), key.begin(), ::tolower);          }          _dict[key] = val;      }  }     string SimpleCommandLineReader::Get(string key) {      return Get(key, "");  }     string SimpleCommandLineReader::Get(string key, string default_value) {      if (!_caseSensitive) {          std::transform(key.begin(), key.end(), key.begin(), ::tolower);      }      if (_dict.count(key) > 0) {          return _dict[key];      }      return default_value;  }
/*      Author: https://helloacm.com      Free to Use, donation is appreciated:  https://helloacm.com/out/paypal  */    #include <iostream>  #include <map>  #include <vector>  #include <algorithm>    using namespace std;    class SimpleCommandLineReader {  public:      SimpleCommandLineReader(int, char**);      SimpleCommandLineReader(int, char**, bool);      string Get(string);      string Get(string, string);    private:      const char SepChar = '=';      vector<string> _args;      map<string, string> _dict;      bool _caseSensitive = false;      void process(void);  };    SimpleCommandLineReader::SimpleCommandLineReader(int argc, char** argv): SimpleCommandLineReader(argc, argv, false)  {    };    SimpleCommandLineReader::SimpleCommandLineReader(int argc, char** argv, bool sensitive){      _caseSensitive = sensitive;      for (int i = 0; i < argc; i ++) {          string cur(argv[i]);          _args.push_back(cur);      }      process();  };    void SimpleCommandLineReader::process(void) {      for (int i = 0; i < _args.size(); i ++) {          string cur = _args[i];          string key = "";          string val = "";          for (int j = 0; j < cur.length(); j ++) {              if (cur[j] == SepChar) {                  key = cur.substr(0, j);                  val = cur.substr(j + 1);                  break;              }          }          if (!_caseSensitive) {              std::transform(key.begin(), key.end(), key.begin(), ::tolower);          }          _dict[key] = val;      }  }    string SimpleCommandLineReader::Get(string key) {      return Get(key, "");  }    string SimpleCommandLineReader::Get(string key, string default_value) {      if (!_caseSensitive) {          std::transform(key.begin(), key.end(), key.begin(), ::tolower);      }      if (_dict.count(key) > 0) {          return _dict[key];      }      return default_value;  }

To demonstrate the usage:

int main(int argc, char** argv) {      SimpleCommandLineReader param(argc, argv);      cout << param.Get("key1") << endl;      cout << param.Get("key2", "default_value_key2") << endl;      cout << param.Get("key3", "default_value_key3") << endl;      return 0;  }
int main(int argc, char** argv) {      SimpleCommandLineReader param(argc, argv);      cout << param.Get("key1") << endl;      cout << param.Get("key2", "default_value_key2") << endl;      cout << param.Get("key3", "default_value_key3") << endl;      return 0;  }

The .h and .cpp C++ code has been maintained at github so feel free to contribute.

 

</div>

 本文由用户 key_zhou 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1456886782500.html
C/C++开发 C/C++