diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-07 08:42:54 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-07 08:42:54 +0100 |
| commit | d51fe89516bb89f0a39f847370a1d43bc684d455 (patch) | |
| tree | 4ea5887611aaaf9d3d25c074b807ee496a20d2c4 /src/test_demo.cc | |
| parent | 0a586bbbe8dc39a21e0edfc7b7f3ec4b72f0bc9b (diff) | |
feat(test_demo): Add validation for command-line options
Adds error handling for unknown or invalid command-line options:
- Unknown options (e.g., --invalid) print error and help, then exit(1)
- Missing arguments (e.g., --resolution without WxH) print error and help
- Invalid format (e.g., --resolution abc) print error and help
Error handling:
- Prints specific error message to stderr
- Shows full help text for reference
- Exits with status code 1 (error)
- --help still exits with status code 0 (success)
Examples of new behavior:
$ test_demo --unknown
Error: Unknown option '--unknown'
[help text displayed]
$ test_demo --resolution
Error: --resolution requires an argument (e.g., 1024x768)
[help text displayed]
$ test_demo --resolution abc
Error: Invalid resolution format 'abc' (expected WxH, e.g., 1024x768)
[help text displayed]
This prevents silent failures and helps users discover correct usage.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/test_demo.cc')
| -rw-r--r-- | src/test_demo.cc | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/src/test_demo.cc b/src/test_demo.cc index 4d2d60b..a93b0b1 100644 --- a/src/test_demo.cc +++ b/src/test_demo.cc @@ -53,6 +53,7 @@ int main(int argc, char** argv) { bool log_peaks_fine = false; #if !defined(STRIP_ALL) + // Early exit for invalid options for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--help") == 0) { print_usage(argv[0]); @@ -61,17 +62,37 @@ int main(int argc, char** argv) { fullscreen_enabled = true; } else if (strcmp(argv[i], "--tempo") == 0) { tempo_test_enabled = true; - } else if (strcmp(argv[i], "--resolution") == 0 && i + 1 < argc) { - const char* res_str = argv[++i]; - int w, h; - if (sscanf(res_str, "%dx%d", &w, &h) == 2) { - width = w; - height = h; + } else if (strcmp(argv[i], "--resolution") == 0) { + if (i + 1 < argc) { + const char* res_str = argv[++i]; + int w, h; + if (sscanf(res_str, "%dx%d", &w, &h) == 2) { + width = w; + height = h; + } else { + fprintf(stderr, "Error: Invalid resolution format '%s' (expected WxH, e.g., 1024x768)\n\n", res_str); + print_usage(argv[0]); + return 1; + } + } else { + fprintf(stderr, "Error: --resolution requires an argument (e.g., 1024x768)\n\n"); + print_usage(argv[0]); + return 1; + } + } else if (strcmp(argv[i], "--log-peaks") == 0) { + if (i + 1 < argc) { + log_peaks_file = argv[++i]; + } else { + fprintf(stderr, "Error: --log-peaks requires a filename argument\n\n"); + print_usage(argv[0]); + return 1; } - } else if (strcmp(argv[i], "--log-peaks") == 0 && i + 1 < argc) { - log_peaks_file = argv[++i]; } else if (strcmp(argv[i], "--log-peaks-fine") == 0) { log_peaks_fine = true; + } else { + fprintf(stderr, "Error: Unknown option '%s'\n\n", argv[i]); + print_usage(argv[0]); + return 1; } } #else |
