-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cpp
182 lines (146 loc) · 4.37 KB
/
app.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "project.h"
#include "timestamp.h"
static const char kVersionInfo[] =
"CLI Example\n"
"\n"
"Built: " MyCpp_TIMESTAMP "\n"
"Project: " MyCpp_VERSION "\n"
"Copyright (C) 2023 Yuhao Gu. All Rights Reserved.";
#include "po.hpp"
#include <array>
#include <iomanip>
#include <iostream>
#include <My/err.hpp>
#include <My/log.hpp>
#include <My/util.hpp>
using namespace My::util;
// ========================================================================== //
// 子命令示例
// ========================================================================== //
namespace {
int
subcmd(int argc, char* argv[])
{
po::options_description od("'subcmd' Options");
od.add_options() //
("help,h", "print help info") //
("output,o", po::value<std::string>(), "output path") //
;
po::positional_options_description pod;
pod.add("output", 1);
po::variables_map vmap;
po::store(
po::command_line_parser(argc, argv).options(od).positional(pod).run(),
vmap);
po::notify(vmap);
if (vmap.count("help") || argc == 1) {
std::cout << od << std::endl;
return 0;
}
std::cout << "output path is '" << vmap["output"].as<std::string>() << "'\n"
<< std::flush;
return 0;
}
} // namespace
// ========================================================================== //
// 配置日志
// ========================================================================== //
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
namespace {
int gLogLevel = My::log::Level::noti;
void
init_log()
{
boost::log::add_common_attributes();
boost::log::add_console_log(std::clog,
boost::log::keywords::format = &My::log::format);
boost::log::core::get()->set_filter(
[](const boost::log::attribute_value_set& attrs) {
return attrs["Severity"].extract<My::log::Level>() >= gLogLevel;
});
}
} // namespace
// ========================================================================== //
// 主函数
// ========================================================================== //
namespace {
struct SubCmd
{
const char *mName, *mInfo;
int (*mFunc)(int argc, char* argv[]);
};
const SubCmd kSubCmds[] = {
{ "subcmd", "subcmd example", &subcmd },
// TODO
};
} // namespace
int
main(int argc, char* argv[])
try {
po::options_description od("Options");
od.add_options() //
("version,v", "print version info") //
("help,h", "print help info") //
("log", povd(gLogLevel), "log level") //
("...", //
po::value<std::vector<std::string>>(), //
"other arguments") //
;
po::positional_options_description pod;
pod.add("...", -1);
std::vector<std::string> opts{ argv[0] };
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-')
opts.emplace_back(argv[i]);
else
break;
}
po::variables_map vmap;
auto parsed = po::command_line_parser(opts)
.options(od)
.positional(pod)
.allow_unregistered()
.run();
po::store(parsed, vmap);
if (vmap.count("help") || argc == 1) {
std::cout << od
<< "\n"
"Sub Commands:\n";
for (auto&& i : kSubCmds)
std::cout << " " << std::left << std::setw(12) << i.mName << i.mInfo
<< '\n';
std::cout << "\n"
"[HINT: use '<subcmd> --help' to get help for sub commands.]\n"
<< std::endl;
return 0;
}
if (vmap.count("version")) {
std::cout << kVersionInfo << std::endl;
return 0;
}
// 如果必须的选项没有指定,在这里会发生错误,因此 version 和 help
// 选项要放在前面。
po::notify(vmap);
if (opts.size() < argc) {
std::string cmd = argv[opts.size()];
for (auto&& i : kSubCmds) {
if (cmd == i.mName)
return i.mFunc(argc - opts.size(), argv + opts.size());
}
std::cout << "invalid sub command '" << cmd << "'." << std::endl;
return 1;
}
}
catch (My::Err& e) {
std::cout << e.what() << ": " << e.info() << std::endl;
return -3;
}
catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
return -2;
}
catch (...) {
std::cout << "UNKNOWN EXCEPTION" << std::endl;
return -1;
}