Skip to content

Commit e47f215

Browse files
authored
Add --help, --version, error on invalid CLI arguments (#64)
* add --help, --version cli arguments * improve help messages, show --help in README
1 parent c4f0384 commit e47f215

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

2048.c

+66-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <signal.h> // defines: signal, SIGINT
1919

2020
#define SIZE 4
21+
#define VERSION "1.0"
2122

2223
// this function receives 2 pointers (indicated by *) so it can set their values
2324
void getColors(uint8_t value, uint8_t scheme, uint8_t *foreground, uint8_t *background)
@@ -425,6 +426,12 @@ int test()
425426
return !success;
426427
}
427428

429+
int version()
430+
{
431+
printf("2048.c version %s\n", VERSION);
432+
return EXIT_SUCCESS;
433+
}
434+
428435
void signal_callback_handler(int signum)
429436
{
430437
printf(" TERMINATED \n");
@@ -434,6 +441,48 @@ void signal_callback_handler(int signum)
434441
exit(signum);
435442
}
436443

444+
struct CliOption
445+
{
446+
char* name;
447+
char* description;
448+
};
449+
struct CliOption cli_options[] = {
450+
{"--help", "Show this help message."},
451+
{"--version", "Show version number."},
452+
{"bluered", "Use a blue-to-red color scheme (requires 256-color terminal support). If unsupported, it will fall back to black-to-white."},
453+
{"blackwhite", "The default color scheme is black-to-white (requires 256-color terminal support)."},
454+
{"test", "Run the test suite."},
455+
};
456+
457+
char* parseArgs(int argc, char* argv[], int* status_code)
458+
{
459+
if (argc == 1)
460+
{
461+
return "blackwhite"; // default color scheme
462+
}
463+
else if (argc == 2)
464+
{
465+
return argv[1];
466+
}
467+
else
468+
{
469+
printf("Invalid number of arguments\n");
470+
*status_code = EXIT_FAILURE;
471+
return "--help";
472+
}
473+
}
474+
475+
int help(int success) {
476+
printf("Usage: 2048 [OPTION]\n");
477+
printf("Play the game 2048 in the console\n\n");
478+
printf("Options:\n");
479+
for (int i = 0; i < sizeof(cli_options) / sizeof(cli_options[0]); i++)
480+
{
481+
printf(" %-12s %s\n", cli_options[i].name, cli_options[i].description);
482+
}
483+
return success;
484+
}
485+
437486
int main(int argc, char *argv[])
438487
{
439488
uint8_t board[SIZE][SIZE];
@@ -442,18 +491,31 @@ int main(int argc, char *argv[])
442491
char c;
443492
bool success;
444493

445-
if (argc == 2 && strcmp(argv[1], "test") == 0)
494+
int status_code = EXIT_SUCCESS;
495+
char* option = parseArgs(argc, argv, &status_code);
496+
if (strcmp(option, "--help") == 0 || strcmp(option, "-h") == 0)
497+
{
498+
return help(status_code);
499+
}
500+
else if (strcmp(option, "--version") == 0 || strcmp(option, "-v") == 0)
501+
{
502+
return version();
503+
}
504+
else if (strcmp(option, "test") == 0)
446505
{
447506
return test();
448507
}
449-
if (argc == 2 && strcmp(argv[1], "blackwhite") == 0)
508+
else if (strcmp(option, "blackwhite") == 0)
450509
{
451510
scheme = 1;
452511
}
453-
if (argc == 2 && strcmp(argv[1], "bluered") == 0)
512+
else if (strcmp(option, "bluered") == 0)
454513
{
455514
scheme = 2;
456-
}
515+
} else {
516+
printf("Invalid option: %s\n", option);
517+
return help(EXIT_FAILURE);
518+
}
457519

458520
// make cursor invisible, erase entire screen
459521
printf("\033[?25l\033[2J");

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ For the blue-to-red color scheme (requires 256 colors):
5252
./2048 bluered
5353
```
5454

55+
Show help message (shows all supported CLI options):
56+
57+
```
58+
./2048 help
59+
```
60+
5561
### Contributing
5662

5763
Contributions are very welcome. Always run the tests before committing using:

0 commit comments

Comments
 (0)