Better error messages in n64graphics (#370)

This commit is contained in:
Jon Crall 2024-10-16 03:19:01 -04:00 committed by GitHub
parent 8a62dfbdac
commit 1bde20d896
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -744,52 +744,85 @@ static int parse_arguments(int argc, char *argv[], graphics_config *config)
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
switch (argv[i][1]) { switch (argv[i][1]) {
case 'c': case 'c':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'c'\n");
return 0;
}
if (!parse_format(&config->pal_format, argv[i])) { if (!parse_format(&config->pal_format, argv[i])) {
ERROR("Error parsing 'c' format");
return 0; return 0;
} }
break; break;
case 'e': case 'e':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'e'\n");
return 0;
}
config->bin_filename = argv[i]; config->bin_filename = argv[i];
config->mode = MODE_EXPORT; config->mode = MODE_EXPORT;
break; break;
case 'f': case 'f':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'f'\n");
return 0;
}
if (!parse_format(&config->format, argv[i])) { if (!parse_format(&config->format, argv[i])) {
ERROR("Error parsing format after 'f'");
return 0; return 0;
} }
break; break;
case 'g': case 'g':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'g'\n");
return 0;
}
config->img_filename = argv[i]; config->img_filename = argv[i];
break; break;
case 'h': case 'h':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'h'\n");
return 0;
}
config->height = strtoul(argv[i], NULL, 0); config->height = strtoul(argv[i], NULL, 0);
break; break;
case 'i': case 'i':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'i'\n");
return 0;
}
config->bin_filename = argv[i]; config->bin_filename = argv[i];
config->mode = MODE_IMPORT; config->mode = MODE_IMPORT;
break; break;
case 'o': case 'o':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'o'\n");
return 0;
}
config->bin_offset = strtoul(argv[i], NULL, 0); config->bin_offset = strtoul(argv[i], NULL, 0);
config->bin_truncate = 0; config->bin_truncate = 0;
break; break;
case 'p': case 'p':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'p'\n");
return 0;
}
config->pal_filename = argv[i]; config->pal_filename = argv[i];
break; break;
case 'P': case 'P':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'P'\n");
return 0;
}
config->pal_offset = strtoul(argv[i], NULL, 0); config->pal_offset = strtoul(argv[i], NULL, 0);
config->pal_truncate = 0; config->pal_truncate = 0;
break; break;
case 's': case 's':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 's'\n");
return 0;
}
if (!parse_encoding(&config->encoding, argv[i])) { if (!parse_encoding(&config->encoding, argv[i])) {
ERROR("Error parsing 's' encoding\n");
return 0; return 0;
} }
break; break;
@ -801,14 +834,19 @@ static int parse_arguments(int argc, char *argv[], graphics_config *config)
exit(0); exit(0);
break; break;
case 'w': case 'w':
if (++i >= argc) return 0; if (++i >= argc) {
ERROR("Not enough arguments after 'w'\n");
return 0;
}
config->width = strtoul(argv[i], NULL, 0); config->width = strtoul(argv[i], NULL, 0);
break; break;
default: default:
ERROR("Error parsing arguments (default)\n");
return 0; return 0;
break; break;
} }
} else { } else {
ERROR("Error parsing arguments (else case)\n");
return 0; return 0;
} }
} }
@ -819,11 +857,13 @@ static int parse_arguments(int argc, char *argv[], graphics_config *config)
static int valid_config(const graphics_config *config) static int valid_config(const graphics_config *config)
{ {
if (!config->bin_filename || !config->img_filename) { if (!config->bin_filename || !config->img_filename) {
ERROR("Invalid bin filename or img filename\n");
return 0; return 0;
} }
if (config->format.format == IMG_FORMAT_CI) { if (config->format.format == IMG_FORMAT_CI) {
if (!config->pal_filename || (config->pal_format.depth != 16) || if (!config->pal_filename || (config->pal_format.depth != 16) ||
(config->pal_format.format != IMG_FORMAT_RGBA && config->pal_format.format != IMG_FORMAT_IA)) { (config->pal_format.format != IMG_FORMAT_RGBA && config->pal_format.format != IMG_FORMAT_IA)) {
ERROR("Invalid format issue\n");
return 0; return 0;
} }
} }
@ -844,6 +884,12 @@ int main(int argc, char *argv[])
int valid = parse_arguments(argc, argv, &config); int valid = parse_arguments(argc, argv, &config);
if (!valid || !valid_config(&config)) { if (!valid || !valid_config(&config)) {
if (!valid){
ERROR("Invalid arguments were given\n");
}
else {
ERROR("The config is invalid\n");
}
print_usage(); print_usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }