-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentReader.h
63 lines (59 loc) · 1.54 KB
/
ArgumentReader.h
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
#ifndef ARGUMENT_READER_H
#define ARGUMENT_READER_H
#include<string>
#include<iostream>
using namespace std;
namespace ArgumentReader
{
enum class ArgType
{
script,
document,
macro_file,
nothing,
error,
};
ArgType get_type(const std::string& arg)
{
if (arg.find("-s") != std::string::npos) return ArgType::script;
else if (arg.find("-d") != std::string::npos) return ArgType::document;
else if (arg.find("-m") != std::string::npos) return ArgType::macro_file;
else if (arg.empty()) return ArgType::nothing;
else return ArgType::error;
}
struct Args
{
Args(const std::string& sp,
const std::string& dp,
const std::string& mp)
{
script_path = sp;
document_path = dp;
macro_file = mp;
}
~Args(){}
std::string script_path, document_path,macro_file;
};
Args* read_args(int argc, char** argv)
{
if (argc == 1)return nullptr;
if (argc > 4) return nullptr;
if (argc != 3 && argc !=4)return nullptr;
using namespace std;
string args[]{ string(argv[1]),string(argv[2]), argc == 4 ? string(argv[3]) : "" };
string script, doc, macro;
for (int i = 0; i < 3; i++)
{
switch (get_type(args[i]))
{
case ArgType::script: script = args[i].substr(2); break;
case ArgType::document: doc = args[i].substr(2); break;
case ArgType::macro_file: macro = args[i].substr(2); break;
case ArgType::error:return nullptr;
};
}
if (script.empty() || doc.empty()) return nullptr; //these are very important args
return new Args(script, doc,macro);
}
};
#endif //ARGUMENT_READER_H