C++ Simple Command Line Parameters Reader


In this 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
    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:

1
2
3
4
5
6
7
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.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
395 words
Last Post: The C# Simple Command Line Parameters Reader
Next Post: How to Show Default Regular Price for Woocommerce/Wordpress based on Sale Price?

The Permanent URL is: C++ Simple Command Line Parameters Reader

Leave a Reply