Compare commits
2 Commits
b3cd8709fb
...
310011a6da
Author | SHA1 | Date |
---|---|---|
BodgeMaster | 310011a6da | |
BodgeMaster | 6a5096dfa3 |
|
@ -18,7 +18,12 @@
|
|||
#include <cstdint>
|
||||
#include <cctype>
|
||||
|
||||
#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] <port>" << std::endl << "<port> may be hexadecimal (prefixed with 0x) or binary (prefixed with 0b)." << std::endl
|
||||
#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] <<host> <port> | -l <port>>" << std::endl << "<port> may be hexadecimal (prefixed with 0x) or binary (prefixed with 0b)." << std::endl
|
||||
|
||||
#define EXIT_SUCCESS 0
|
||||
#define EXIT_RUNTIME 1
|
||||
#define EXIT_USAGE 2
|
||||
#define EXIT_UNIMPLEMENTED 3
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
|
@ -27,10 +32,11 @@ int main(int argc, char* argv[]){
|
|||
bool ipv6 = true;
|
||||
bool tcp = true;
|
||||
bool udp = true;
|
||||
bool server = false;
|
||||
uint16_t port;
|
||||
if (argc<2) {
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
for (int i=1; i<argc; i++) {
|
||||
std::string argument(argv[i]);
|
||||
|
@ -54,13 +60,22 @@ int main(int argc, char* argv[]){
|
|||
udp = true;
|
||||
continue;
|
||||
}
|
||||
if (argument=="-l") {
|
||||
server = true;
|
||||
continue;
|
||||
}
|
||||
if (argument=="-h") {
|
||||
usage;
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (argument=="--help") {
|
||||
usage;
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (!server && i==argc-2) {
|
||||
//TODO: check if valid host
|
||||
std::cerr << "Host argument parsing is not yet implemented." << std::endl;
|
||||
return EXIT_UNIMPLEMENTED;
|
||||
}
|
||||
if (i==argc-1) {
|
||||
if (argument.substr(0, 2)=="0b" || argument.substr(0, 2)=="0B") {
|
||||
|
@ -71,13 +86,13 @@ int main(int argc, char* argv[]){
|
|||
} else {
|
||||
std::cerr << argument << " is not a valid port." << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
}
|
||||
if (argument.length()>18) {
|
||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 2);
|
||||
} else if (argument.substr(0, 2)=="0x" || argument.substr(0, 2)=="0X") {
|
||||
|
@ -86,13 +101,13 @@ int main(int argc, char* argv[]){
|
|||
if (std::isxdigit(digit)==0) {
|
||||
std::cerr << argument << " is not a valid port." << digit << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
}
|
||||
if (argument.length()>6) {
|
||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 16);
|
||||
} else {
|
||||
|
@ -101,7 +116,7 @@ int main(int argc, char* argv[]){
|
|||
if (std::isdigit(digit)==0) {
|
||||
std::cerr << argument << " is not a valid port." << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
}
|
||||
// using unsigned long here because that's how stoul()
|
||||
|
@ -110,14 +125,14 @@ int main(int argc, char* argv[]){
|
|||
if (argumentNumber > 65535) {
|
||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
port = (uint16_t) argumentNumber;
|
||||
}
|
||||
break; // last argument anyway
|
||||
}
|
||||
usage;
|
||||
return 1;
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
// Argument parsing end ############################################
|
||||
|
||||
|
|
Loading…
Reference in New Issue