-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.cpp
More file actions
64 lines (52 loc) · 1.53 KB
/
cli.cpp
File metadata and controls
64 lines (52 loc) · 1.53 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
#include "cli.h"
void WebserverCLI::print_help(std::string binary){
std::cout << "usage: " << binary << " [--port port | --directory directory]" << std::endl << std::endl;
std::cout << " -h, --help Show this help page" << std::endl;
std::cout << " -p, --port Select the port" << std::endl;
std::cout << " -d, --directory Select the directory to serve" << std::endl;
exit(-1);
}
WebserverCLI::WebserverCLI(int argc, char* argv[]){
if(argc <= 1){
return;
}
std::string binary = std::string(argv[0]);
std::string arg;
for(int i = 1; i < argc; i++){
arg = std::string(argv[i]);
if(arg == "--port" || arg == "-p"){
if(i >= argc)
print_help(binary);
int input;
try{
input = std::stoi(argv[i+1]);
if(input >= 1024 && input <= 65536){
this->port = input;
i++;
continue;
}
}catch(...){}
std::cerr << "[-] Invalid port '" << argv[i+1] << "' (1024-65536)." << std::endl;
exit(-1);
}else if(arg == "--directory" || arg == "-d"){
if(i >= argc)
print_help(binary);
std::filesystem::directory_entry dir(argv[i+1]);
if(dir.is_directory()){
this->dir = std::string(argv[i+1]);
i++;
continue;
}
std::cerr << "[-] Invalid directory '" << argv[i+1] << "'." << std::endl;
exit(-1);
}else{
print_help(binary);
}
}
}
std::string WebserverCLI::getDirectory(){
return this->dir;
}
int WebserverCLI::getPort(){
return this->port;
}