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

key_zhou 8年前

来自: 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>