-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmonitor_base.cpp
More file actions
65 lines (60 loc) · 2.03 KB
/
Copy pathmonitor_base.cpp
File metadata and controls
65 lines (60 loc) · 2.03 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
#include <iostream>
#include <getopt.h>
#include "log.h"
#include "tools/monitor_base.h"
using namespace std;
namespace xpu {
static void Usage()
{
cerr << "Usage: xpu-monitor [option [value]]\n";
cerr << "\n";
cerr << "Valid options:\n";
cerr << " -p,--period The time period in seconds used to calculate computing power\n";
cerr << " range 1 ~ 86400, default 60 (1 minute)\n";
cerr << " -o,--output Output format, one of: json|table\n";
cerr << " -h,--help Print usage information\n";
cerr << endl;
}
int ParseArgs(Args &args, int argc, char *const argv[])
{
int nextOption;
int tmpValue;
const char *const shortOptions = "o:p:h";
const struct option longOptions[] = {
{"output", 1, nullptr, 'o'},
{"period", 1, nullptr, 'p'},
{"help", 0, nullptr, 'h'},
{nullptr, 0, nullptr, 0},
};
while ((nextOption = getopt_long(argc, argv, shortOptions, longOptions, nullptr)) != -1) {
switch (nextOption) {
case 'o':
if (optarg == string_view("json")) {
args.format = OutputFormat::JSON;
} else if (optarg == string_view("table")) {
args.format = OutputFormat::TABLE;
} else {
cerr << "format value is invalid: " << optarg << ", the value must be json or table\n";
Usage();
return RET_FAIL;
}
break;
case 'p':
tmpValue = atoi(optarg);
if (tmpValue >= PERIOD_MIN && tmpValue <= PERIOD_MAX) {
args.period = tmpValue;
} else {
cerr << "option value is invalid: " << optarg << ", the value must range in 1 ~ 86400\n";
Usage();
return RET_FAIL;
}
break;
case 'h':
default:
Usage();
return RET_FAIL;
}
}
return RET_SUCC;
}
}