Add argument parsing

This commit is contained in:
Ultracoolguy 2020-11-07 18:21:04 -04:00 committed by Eyal Sawady
parent aae642876e
commit 96448759d6
No known key found for this signature in database
GPG Key ID: 604D3459E53A9952
1 changed files with 28 additions and 2 deletions

30
main.c
View File

@ -15,6 +15,7 @@
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#define NUM_PLAYERS 10
#define NUM_SHORT 6
@ -206,6 +207,14 @@ const char map[] =
"|/----------------|--------------|--------------|--------|-------/\n"
;
const char usage[] =
"Usage: among-sus [-p <port>] [-h]\n"
"among-sus:\tAmong Us, but it's a text adventure\n"
"\n"
"-p, \t\tSet port number\n"
"-h, \t\tDisplay this message\n"
;
enum player_state {
PLAYER_STATE_ALIVE,
PLAYER_STATE_VENT, // TODO: implement vents
@ -1346,13 +1355,30 @@ welcome_player(int fd)
}
int
main(void)
main(int argc, char *argv[])
{
// Set default settings
state.impostor_cooldown = 1;
int listen_fd, listen6_fd, new_fd, i;
uint16_t port = 1234;
char *endptr = NULL;
int opt;
while ((opt = getopt(argc, argv, "hp:")) != -1) {
switch (opt) {
case 'p':
port = strtol(optarg, &endptr, 10);
break;
case 'h':
printf("%s", usage);
exit(EXIT_SUCCESS);
case '?':
printf("%s", usage);
exit(EXIT_FAILURE);
}
}
int listen_fd, listen6_fd, new_fd, i;
socklen_t client_size;
struct sockaddr_in listen_addr, client_addr;
struct sockaddr_in6 listen6_addr;