C++ 簡單的命令行參數閱讀器

key_zhou 8年前發布 | 10K 次閱讀 C/C++開發 C/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.

 

 本文由用戶 key_zhou 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!