[clink] Improve program argument system

git-svn-id: svn://kolibrios.org@9088 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Magomed Kostoev (mkostoevr) 2021-07-25 20:38:50 +00:00
parent be0e7536fb
commit 53e6a25eb8

View File

@ -634,10 +634,6 @@ static ObjectIr parse_objects(int argc, char **argv) {
return ir; return ir;
} }
int first_arg(int argc, char **argv, const char *arg) {
return argc >= 2 && !strcmp(argv[1], arg);
}
int arg_got_flag(int argc, char **argv, ...) { int arg_got_flag(int argc, char **argv, ...) {
char *arg_names[8]; char *arg_names[8];
int arg_name_c = 0; int arg_name_c = 0;
@ -654,38 +650,74 @@ int arg_got_flag(int argc, char **argv, ...) {
va_end(ap); va_end(ap);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
// If an argumetns was handled already then it's NULL here
if (argv[i] == NULL) {
continue;
}
for (int arg_name_i = 0; arg_name_i < arg_name_c; arg_name_i++) { for (int arg_name_i = 0; arg_name_i < arg_name_c; arg_name_i++) {
char *arg_name = arg_names[arg_name_i]; char *arg_name = arg_names[arg_name_i];
if (!strcmp(argv[i], arg_name)) { if (!strcmp(argv[i], arg_name)) {
argv[i] = NULL; // Do not handle this argument as a input name argv[i] = NULL; // Do not handle this argument as a input name
return 1; return i;
} }
} }
} }
return 0; return 0;
} }
int main(int argc, char **argv) { char *arg_got_param(int argc, char **argv, char *arg) {
const char *outname = "a.out.obj"; int i = arg_got_flag(argc, argv, arg, 0);
if (i == 0) {
return NULL;
}
if (argc >= 3 && !strcmp(argv[1], "-o")) { if (i + 1 >= argc) {
outname = argv[2]; printf("Warning: %s parameter expects a value (like %s <value>)", arg, arg);
argv += 2; return NULL;
argc -= 2; }
} else if (first_arg(argc, argv, "-h") char *result = argv[i + 1];
|| first_arg(argc, argv, "-help") argv[i + 1] = NULL;
|| first_arg(argc, argv, "--help") return result;
|| argc == 1) { }
printf("Usage cases:\n");
printf(" %s [-o outname] object files list\n", argv[0]); int usage(char *name, char *remark) {
printf(" Link COFF files into one, optionally set name of output\n"); if (remark) {
printf(" %s [-h|-help|--help]\n", argv[0]); printf("Error: %s\n\n", remark);
printf(" Output this help\n"); }
return 0; printf("Usage: %s [option]... [object file name]...\n", name);
printf(" Link multiple COFF files into one\n");
printf("\n");
printf("Options:\n");
printf(" -o <outname> Output into <outname>\n");
printf(" -v, --verbose Emit information logs\n");
printf(" -h, --help Output this help and exit\n");
return 0;
}
int main(int argc, char **argv) {
if (arg_got_flag(argc, argv, "-h", "-help", "--help", 0)) {
return usage(argv[0], NULL);
}
const char *outname = arg_got_param(argc, argv, "-o");
if (outname == NULL) {
outname = "a.out.obj";
} }
emit_logs = arg_got_flag(argc, argv, "-v", "-verbose", "--verbose", 0); emit_logs = arg_got_flag(argc, argv, "-v", "-verbose", "--verbose", 0);
// After handling arguments there only leaven unhandled ones
// They should be names if inputs. But if there's no input - exit
int input_file_count = 0;
for (int i = 1; i < argc; i++) {
if (argv[i] != NULL) {
input_file_count++;
}
}
if (input_file_count == 0) {
return usage(argv[0], "No input file names supplied");
}
ObjectIr ir = parse_objects(argc, argv); ObjectIr ir = parse_objects(argc, argv);
build(&ir, outname); build(&ir, outname);
return 0; return 0;