diff options
author | Tom Stellard <thomas.stellard@amd.com> | 2015-04-13 19:47:13 +0000 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2015-04-13 14:16:20 -0700 |
commit | 4ed9fa04cf125b63db0555c5af1a96f5061d52d4 (patch) | |
tree | 7b75d36b125d1df69b205e05c01f2b72b440e984 | |
parent | 6d6109f45b9a134ce269c0d8a9eb738c4c93e9e9 (diff) |
run.c: Use getopt for argument parsing
Reviewed-by: Matt Turner <mattst88@gmail.com>
-rw-r--r-- | run.c | 60 |
1 files changed, 39 insertions, 21 deletions
@@ -1,6 +1,7 @@ /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ /* * Copyright © 2014 Intel Corporation + * Copyright © 2015 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -272,35 +273,52 @@ const struct platform platforms[] = { "bdw", "0x162E", }; +void print_usage(const char *prog_name) +{ + fprintf(stderr, + "Usage: %s [-p <platform>] <directories and *.shader_test files>\n", + prog_name); +} + int main(int argc, char **argv) { - if (argc >= 2 && strcmp(argv[1], "-p") == 0) { - const struct platform *platform; - for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) { - if (strcmp(argv[2], platforms[i].name) == 0) { - platform = platforms + i; - break; + + int opt; + + while((opt = getopt(argc, argv, "p:")) != -1) { + switch(opt) { + case 'p': { + const struct platform *platform = NULL; + for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) { + if (strcmp(optarg, platforms[i].name) == 0) { + platform = platforms + i; + break; + } + } + + if (platform == NULL) { + fprintf(stderr, "Invalid platform.\nValid platforms are:"); + for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) + fprintf(stderr, " %s", platforms[i].name); + fprintf(stderr, "\n"); + return -1; } - } - if (platform == NULL) { - fprintf(stderr, "Invalid platform.\nValid platforms are:"); - for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) - fprintf(stderr, " %s", platforms[i].name); - fprintf(stderr, "\n"); + printf("### Compiling for %s ###\n", platform->name); + setenv("INTEL_DEVID_OVERRIDE", platform->pci_id, 1); + break; + } + default: + fprintf(stderr, "Unknown option: %x\n", opt); + print_usage(argv[0]); return -1; } - - printf("### Compiling for %s ###\n", platform->name); - setenv("INTEL_DEVID_OVERRIDE", platform->pci_id, 1); - argv += 2; - argc -= 2; } - if (unlikely(argc < 2)) { - fprintf(stderr, "Usage: %s [-p <platform>] <directories and *.shader_test files>\n", - argv[0]); + if (unlikely(optind >= argc)) { + fprintf(stderr, "No directories specified\n"); + print_usage(argv[0]); return -1; } @@ -465,7 +483,7 @@ main(int argc, char **argv) } shader_test = malloc(shader_test_size * sizeof(struct shader_test)); - for (int i = 1; i < argc; i++) { + for (int i = optind; i < argc; i++) { ftw(argv[i], gather_shader_test, 100); } |