-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx_options.cpp
80 lines (69 loc) · 2.16 KB
/
x_options.cpp
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
73
74
75
76
77
78
79
//
// x_options.cpp
// x
//
// Created by Daher Alfawares on 8/28/17.
// Copyright © 2017 Daher Alfawares. All rights reserved.
//
#include "x_options.hpp"
#include "x_table.hpp"
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using namespace x;
options::options(int argc, const char * argv[]){
for(int i=1; i< argc; i++)
this->arguments.push_back(argv[i]);
}
void options::map_to(option::map option_map){
this->map = option_map;
option::map::iterator option_iterator = map.end();
for(auto argument : this->arguments){
// what is it?
if(argument[0] == '-'){
// then it's an option.
// find it in the map.
option_iterator = this->map.find(argument);
if( option_iterator == this->map.end()){
std::cout << "Invalid switch: " << argument << std::endl;
continue;
}
auto option_name = option_iterator->first;
option& option_object = option_iterator->second;
option_object.enable();
} else {
if(option_iterator==map.end()){
std::cout << "Ignoring " << argument << std::endl;
continue;
}
// it's a value
auto option_name = option_iterator->first;
option& option_object = option_iterator->second;
option_object += argument;
}
}
}
std::string options::print(){
std::stringstream stream;
x::table out("available options:");
out("switch")("description")++;
for(auto iter:map){
out(iter.first)(iter.second.description())++;
}
stream << "Usage: -[switch] value1 value2 ..." << std::endl;
stream << out;
return stream.str();
}
std::string options::print_values(){
std::stringstream stream;
x::table out("available options:");
out("switch")("value")++;
for(auto iter:map){
std::stringstream stream;
for(auto value:iter.second.values()) stream << "\"" << value << "\" ";
out(iter.first)(stream.str())++;
}
stream << out;
return stream.str();
}