-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLI.cc
99 lines (91 loc) · 2.94 KB
/
CLI.cc
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
/**
* @author: Laplace825
* @date: 2024-06-28T10:23:40
* @lastmod: 2024-07-19T20:26:51
* @description: the main file to deal command line args
* @filePath: /cpp-tiny-json/CLI.cc
* @lastEditor: Laplace825
* @ MIT license
*/
#include <iostream>
#include <string_view>
#include "tjson.hpp"
#include "tjson/tjfile.hpp"
#include "tjson/tjprint.hpp"
using namespace lap::tjson;
TJsonFile tjfile;
TJson tjson;
void help_msg() {
std::cout << "\033[1;32mcan only read json file with '{' begin\033[0m\n"
"[command line args]:\n"
" -h, --help: show this help message\n"
" -r, --read: read which file\n"
" -s, --store: store to which file\n"
" -p, --print: print the json object\n"
" -f, --find: find the key's value in json object\n"
"[example]:\n"
"tjson -r ./file.json\n -s ./file_store.json\n";
std::cout << "Now in path \033[1;32m"
<< std::filesystem::current_path().c_str() << "\033[0m\n";
}
void what_to_do(
std::string_view command, std::string_view readStoreOrFind = "") {
if (command == "-h" || command == "--help") {
help_msg();
}
else if (command == "-r" || command == "--read") {
tjson.clear();
tjfile.readJsonFile(readStoreOrFind);
tjson.setJsonStr(tjfile.getJsonStr());
}
else if (command == "-s" || command == "--store") {
tjfile.dumpJsonObj2File(tjson, readStoreOrFind);
}
else if (command == "-p" || command == "--print") {
tjson.println();
}
else if (command == "-f" || command == "--find") {
auto findResult = tjson[readStoreOrFind];
if (findResult != TJsonObj{}) {
std::cout << "\033[1;32m" << readStoreOrFind
<< "\033[0m:" << findResult << std::endl;
}
else {
std::cout << "null, \033[1;34mmaybe you should read json file "
"first\033[0m\n"
"[example] tjson -r ./file.json -f \"lap\"\n";
}
}
else {
help_msg();
}
}
auto main(int argc, char* argv[]) -> signed {
if (argc == 1) {
help_msg();
return 0;
}
std::vector< std::string_view > args(
argv + 1, argv + argc); // store all the args
for (size_t i = 0; i < args.size();) {
if (args[i] == "-h" || args[i] == "--help") {
what_to_do(args[i++]);
}
if (args[i] == "-p" || args[i] == "--print") {
what_to_do(args[i++]);
}
if (args[i] == "-f" || args[i] == "--find") {
what_to_do(args[i], args[i + 1]);
i += 2;
}
if (args[i] == "-r" || args[i] == "--read") {
what_to_do(args[i], args[i + 1]);
i += 2;
}
if (args[i] == "-s" || args[i] == "--store") {
what_to_do(args[i], args[i + 1]);
i += 2;
}
}
return 0;
}