Skip to content

Commit e388c3d

Browse files
committed
test: add --help for command-line options
Add a help message for the test suite, documenting available options, defaults, and backward-compatible positional arguments.
1 parent c39699d commit e388c3d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/unit_test.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ static int parse_arg(const char* key, const char* value, struct tf_framework* tf
7474
return 0;
7575
}
7676

77+
static void help(void) {
78+
printf("Usage: ./tests [options]\n\n");
79+
printf("Run the test suite for the project with optional configuration.\n\n");
80+
printf("Options:\n");
81+
printf(" --help, -h Show this help message\n");
82+
printf(" --jobs=<num>, -j=<num> Number of parallel worker processes (default: 0 = sequential)\n");
83+
printf(" --iterations=<num>, -i=<num> Number of iterations for each test (default: 16)\n");
84+
printf(" --seed=<hex> Set a specific RNG seed (default: random)\n");
85+
printf("\n");
86+
printf("Notes:\n");
87+
printf(" - All arguments must be provided in the form '--key=value', '-key=value' or '-k=value'.\n");
88+
printf(" - Single or double dashes are allowed for multi character options.\n");
89+
printf(" - Unknown arguments are reported but ignored.\n");
90+
printf(" - Sequential execution occurs if -jobs=0 or unspecified.\n");
91+
printf(" - Iterations and seed can also be passed as positional arguments before any other argument for backward compatibility.\n");
92+
}
93+
7794
static int parse_jobs_count(const char* key, const char* value, struct tf_framework* tf) {
7895
char* ptr_val;
7996
long val = strtol(value, &ptr_val, 10); /* base 10 */
@@ -157,6 +174,11 @@ static int read_args(int argc, char** argv, int start, struct tf_framework* tf)
157174

158175
eq = strchr(raw_arg, '=');
159176
if (!eq || eq == raw_arg + 1) {
177+
/* Allowed options without value */
178+
if (strcmp(key, "h") == 0 || strcmp(key, "help") == 0) {
179+
tf->args.help = 1;
180+
return 0;
181+
}
160182
fprintf(stderr, "Invalid arg '%s': must be -k=value or --key=value\n", raw_arg);
161183
return -1;
162184
}
@@ -272,6 +294,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
272294
/* Initialize command-line options */
273295
tf->args.num_processes = 0;
274296
tf->args.custom_seed = NULL;
297+
tf->args.help = 0;
275298

276299
/* Disable buffering for stdout to improve reliability of getting
277300
* diagnostic information. Happens right at the start of main because
@@ -303,6 +326,11 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
303326
if (read_args(argc, argv, named_arg_start, tf) != 0) {
304327
return EXIT_FAILURE;
305328
}
329+
330+
if (tf->args.help) {
331+
help();
332+
exit(EXIT_SUCCESS);
333+
}
306334
}
307335

308336
return EXIT_SUCCESS;

src/unit_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ struct tf_args {
7474
int num_processes;
7575
/* Specific RNG seed */
7676
const char* custom_seed;
77+
/* Whether to print the help msg */
78+
int help;
7779
};
7880

7981
/* --------------------------------------------------------- */

0 commit comments

Comments
 (0)