|
|
@@ -4,13 +4,31 @@
|
|
|
|
|
|
#include "args.h"
|
|
|
|
|
|
-static void handle_version() {
|
|
|
+static void check_bounds(const char* arg_name, const int i, const int argc) {
|
|
|
+ if (i + 1 >= argc) {
|
|
|
+ printf("%s needs an argument. See: sanke --help\n", arg_name);
|
|
|
+ exit(0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void show_version() {
|
|
|
printf("Sanke version %s\n", VERSION);
|
|
|
exit(0);
|
|
|
}
|
|
|
|
|
|
+static void show_help() {
|
|
|
+ printf(
|
|
|
+ "USAGE: sanke <options>\n\n"
|
|
|
+ "Options:\n"
|
|
|
+ " --width, -w: Set the width (10 - 50) of the board. Default 15.\n"
|
|
|
+ " --height, -h: Set the height (10 - 50) of the board. Default 15.\n"
|
|
|
+ " --speed, -s: Set the speed (1 - 1000) of the game in squares per second. Default 7.\n"
|
|
|
+ " --help : Display this help text.\n"
|
|
|
+ );
|
|
|
+ exit(0);
|
|
|
+}
|
|
|
+
|
|
|
static void set_width(Arguments* args, char* width_str) {
|
|
|
- printf("width_str: %s\n", width_str);
|
|
|
int width = atoi(width_str);
|
|
|
if (width < 10) {
|
|
|
printf("Width must be between 10 and 50. Was %d.\n", width);
|
|
|
@@ -23,7 +41,7 @@ static void set_width(Arguments* args, char* width_str) {
|
|
|
static void set_height(Arguments* args, char* height_str) {
|
|
|
int height = atoi(height_str);
|
|
|
if (height < 10) {
|
|
|
- printf("Width must be between 10 and 50. Was %d.\n", height);
|
|
|
+ printf("Width must be between 10 and 50. Was %s.\n", height_str);
|
|
|
exit(0);
|
|
|
}
|
|
|
|
|
|
@@ -44,7 +62,7 @@ Arguments cmd_args(int argc, char** argv) {
|
|
|
Arguments args = {
|
|
|
.width = 15,
|
|
|
.height = 15,
|
|
|
- .sleep_ms = 150
|
|
|
+ .sleep_ms = 150,
|
|
|
};
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
@@ -53,13 +71,20 @@ Arguments cmd_args(int argc, char** argv) {
|
|
|
|| strcmp(argv[i], "-v") == 0
|
|
|
)
|
|
|
{
|
|
|
- handle_version();
|
|
|
+ show_version();
|
|
|
+
|
|
|
+ } else if (
|
|
|
+ strcmp(argv[i], "--help") == 0
|
|
|
+ )
|
|
|
+ {
|
|
|
+ show_help();
|
|
|
|
|
|
} else if (
|
|
|
strcmp(argv[i], "--width") == 0
|
|
|
|| strcmp(argv[i], "-w") == 0
|
|
|
)
|
|
|
{
|
|
|
+ check_bounds("width", i, argc);
|
|
|
set_width(&args, argv[i + 1]);
|
|
|
|
|
|
} else if (
|
|
|
@@ -67,6 +92,7 @@ Arguments cmd_args(int argc, char** argv) {
|
|
|
|| strcmp(argv[i], "-h") == 0
|
|
|
)
|
|
|
{
|
|
|
+ check_bounds("height", i, argc);
|
|
|
set_height(&args, argv[i + 1]);
|
|
|
|
|
|
} else if (
|
|
|
@@ -74,7 +100,9 @@ Arguments cmd_args(int argc, char** argv) {
|
|
|
|| strcmp(argv[i], "-s") == 0
|
|
|
)
|
|
|
{
|
|
|
+ check_bounds("speed", i, argc);
|
|
|
set_speed(&args, argv[i + 1]);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|