-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgramOptions.cpp
More file actions
138 lines (110 loc) · 4.12 KB
/
ProgramOptions.cpp
File metadata and controls
138 lines (110 loc) · 4.12 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* ProgramOptions.cpp
* Copyright (C) 2016 Nemanja Mićović <nmicovic@outlook.com>
*
* Distributed under terms of the MIT license.
*/
#include "ProgramOptions.hpp"
#include "GlobalContainers.hpp"
#include "color.h"
#include <sstream>
#include <iomanip>
namespace opt = boost::program_options;
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
namespace vlang {
namespace util {
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
bool ProgramOptions::is_init = false;
ProgramOptions& ProgramOptions::get() {
static ProgramOptions instance;
return instance;
}
const std::vector<std::string>& ProgramOptions::input() const {
return m_vm["input-file"].as<std::vector<std::string>>();
}
std::string ProgramOptions::first_input_file() const {
if (m_vm.count("input-file"))
return m_vm["input-file"].as<std::vector<std::string>>()[0];
else return "stdin";
}
bool ProgramOptions::contains_input_files() const {
if (m_vm.count("input-file"))
return true;
else return false;
}
bool ProgramOptions::emit_source() const {
return m_vm["emit-source"].as<bool>();
}
void ProgramOptions::set_input(opt::variables_map vm) {
m_vm = vm;
}
const boost::program_options::variables_map& ProgramOptions::var_map() const {
return m_vm;
}
std::string ProgramOptions::output_path() const {
return m_vm["output"].as<std::string>();
}
std::string ProgramOptions::show_state() const {
std::stringstream ss;
std::string separator = std::string(BOLDRED) + DRAGON_SEPARATOR() + std::string(RESET);
// Showing input
ss << separator << std::endl << BOLDBLUE;
ss << "Input: ";
if (contains_input_files()) {
ss << std::endl << BOLDWHITE;
for (auto &a : input())
ss << "* " << a << std::endl;
ss << RESET;
} else {
ss << BOLDWHITE << "stdin" << RESET << std::endl;
}
// Showing output
ss << BOLDBLUE << "Output: " << BOLDWHITE << output_path() << std::endl;
ss << RESET << separator << std::endl;
return ss.str();
}
bool ProgramOptions::syntax_highlight() const {
return m_vm["color-dump"].as<bool>();
}
bool ProgramOptions::emit_llvm() const {
return m_vm["emit-llvm"].as<bool>();
}
void ProgramOptions::init(int argc, char** argv) {
if (ProgramOptions::get().is_init) {
std::cerr << "Warning! Detected multiple init of ProgramOptions!" << std::endl;
return;
}
opt::options_description desc("All options");
desc.add_options()
("help", "produce help message")
//("optimization", opt::value<std::string>()->default_value(""), "optimization level")
("input-file,i", opt::value<std::vector<std::string> >(), "input .vala file")
("output,o", opt::value<std::string>()->default_value("a.out"), " executable output path and name")
("emit-source,s", opt::value<bool>()->default_value(false), " shows the parsed source code")
("color-dump,C", opt::value<bool>()->default_value(false), " if code is shown, this option gives it syntax highlight")
("emit-llvm,l", opt::value<bool>()->default_value(true), " shows llvm ir on stdout")
;
// Let's make any given unspecified argument as input file
opt::positional_options_description po_desc;
po_desc.add("input-file", -1);
opt::variables_map vm;
// TODO: Handle the cases when we are passed a dumb argument.
// It seems LLVM has turned off exceptions which I need in order to acomplish this.
try {
opt::store(opt::command_line_parser(argc, argv).options(desc).positional(po_desc).run(), vm);
} catch (...) {
std::cout << desc << std::endl;
exit(0);
}
opt::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
exit(0);
}
ProgramOptions::get().is_init = true;
ProgramOptions::get().set_input(vm);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
} // ;util
} // ;vlang
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-