-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4_args.c
45 lines (38 loc) · 1.25 KB
/
connect4_args.c
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
#include <stdlib.h>
#include "connect4_args.h"
const char *argp_program_version = "Connect Four version 1.0";
char doc[] = "Connect Four: Place 4 tiles in a row vertically, horizontally, or diagonally to win.";
const struct argp_option options[] = {
{"width", 'w', "WIDTH", 0, "Set the width (number of columns) of the Connect Four game board."},
{"height", 'h', "HEIGHT", 0, "Set the height (number of rows) of the Connect Four game board."},
{"square", 's', "SQUARE", 0, "Set the width and height of the Connect Four game board."},
{"connect", 'c', "CONNECT", 0, "Set the number of spots in a row needed to win."},
{ 0 }
};
// Contains how to parse arguments
const struct argp argp = { options, parse_opt, NULL, doc };
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
struct arguments *arguments = state->input;
switch (key) {
case 'w':
arguments->cols = atoi(arg);
break;
case 'h':
arguments->rows = atoi(arg);
break;
case 's':
arguments->rows = arguments->cols = atoi(arg);
break;
case 'c':
arguments->connect = atoi(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
void parse_args(int argc, char **argv, struct arguments *arguments)
{
argp_parse(&argp, argc, argv, 0, 0, arguments);
}